且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Javascript文本更改无法正常工作

更新时间:2023-09-03 14:35:40

你的代码很好,除了你需要调用你的thankyou()函数。下面的代码显示了如何从HTML按钮标记onClick事件中调用它。我还添加了HTML 标签属性target =_ blank,因此下载将在新菜单中打开。



Your code is fine, except you need to call your thankyou() function. The code below shows how to call it from within the HTML button tag onClick event. I also added the HTML tag attribute target="_blank" so the download will open in a new menu.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
<script> function thankyou() {
              var words = "Thanks for downloading";
              document.getElementById("thanks").textContent = words;
              } </script>
 </HEAD>
 <BODY>

<h2><a href="file.exe" download="" target="_blank" style="color: #13600d">
<button width="200" height="100" onclick="thankyou();">download</button></a> </h2>
<span id="thanks"> </span>
 </BODY>
</HTML>


将事件附加到按钮cl ick



attach the event to the button click

<a href="file.exe" download="" style="color: #13600d">
           <button width="200" height="100" onclick="thankyou()">download </button>
       </a>





textContent IE 9支持[ ^ ]属性+只有这样才能更好地使用 innerHTML [ ^ ] property



textContent[^] property is supported in IE 9+ only so better to use innerHTML [^] property

document.getElementById("thanks").innerText = words;