Javascript文本更改无法正常工作 [英] Javascript text changing not working

查看:92
本文介绍了Javascript文本更改无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我创建了一个html文件并插入了一个按钮,指的是这样下载文件:

Hello,
I created a html file and inserted a button that refers to download a file this way:

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



我希望按钮是单击并下载启动浏览器将显示感谢您下载,所以我尝试了这个:


I wanted that when the button is clicked and the download starts the browser will show "Thank you for downloading" so I tried this:

<span id="thanks"> </span>




<script> function thankyou() {
              var words = "Thanks for downloading";
              document.getElementById("thanks").textContent = words;
              } </script>



我点击按钮,文件下载开始但跨文本没有变化。



我尝试了什么:



我尝试找到一些解决方案,例如将textContent更改为innerHTML或innerText,但它没有'工作。

我使用谷歌浏览器作为我的浏览器。

谢谢,

Dvir


I clicked the button, the file download started but the span text didn't change.

What I have tried:

I tried finding some solutions, like change textContent to innerHTML or to innerText, but it didn't work.
I use Google Chrome as my browser.
Thanks,
Dvir

推荐答案

你的代码很好,除了你需要调用你的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;


这篇关于Javascript文本更改无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆