使用 JavaScript 更改标签文本
为什么以下对我不起作用?
<script>
document.getElementById('lbltipAddedComment').innerHTML = 'Your tip has been submitted!';
</script>
<label id="lbltipAddedComment"></label>
Why doesn't the following work for me?
<script>
document.getElementById('lbltipAddedComment').innerHTML = 'Your tip has been submitted!';
</script>
<label id="lbltipAddedComment"></label>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
因为您的脚本在标签存在于页面(在 DOM 中)之前运行。将脚本放在标签后面,或者等到文档完全加载(使用 OnLoad 函数,例如 <代码>jQuery Ready() 或 http://www.webreference .com/programming/javascript/onloads/)
这不起作用:
这将起作用:
这个示例(jsfiddle link) 保持顺序(首先是脚本,然后是标签)并使用 onLoad:
Because your script runs BEFORE the label exists on the page (in the DOM). Either put the script after the label, or wait until the document has fully loaded (use an OnLoad function, such as the
jQuery ready()
or http://www.webreference.com/programming/javascript/onloads/)This won't work:
This will work:
This example (jsfiddle link) maintains the order (script first, then label) and uses an onLoad:
您是否尝试过
.innerText
或.value
而不是.innerHTML
?Have you tried
.innerText
or.value
instead of.innerHTML
?因为执行脚本时并没有加载标签元素。交换标签和脚本元素,它就会起作用:
Because a label element is not loaded when a script is executed. Swap the label and script elements, and it will work:
这是使用 jQuery 更改标签文本的另一种方法:
查看 JsFiddle 示例
Here is another way to change the text of a label using jQuery:
Check the JsFiddle example
请改用
.textContent
。我也在努力改变标签的价值,直到我尝试了这个。
如果这不能解决问题,请尝试检查对象以查看可以通过使用
console.dir
将其记录到控制台来设置哪些属性,如以下问题所示:如何将 HTML 元素记录为 JavaScript 对象?Use
.textContent
instead.I was struggling with changing the value of a label as well, until I tried this.
If this doesn't solve try inspecting the object to see what properties you can set by logging it to the console with
console.dir
as shown on this question: How can I log an HTML element as a JavaScript object?使用
.innerText
应该可以。Using
.innerText
should work.试试这个:
Try this:
显然有几种方法可以更改标签的文本,我在下面找到了这个链接页面,其中对每种方法都有解释(文本很多,所以我在这里只附加了示例)。
就我而言,即使在上传标签后使用 javascript 代码,它们也不起作用,因为我使用 ESP32 作为服务器,并使用 Arduino IDE 上传页面,出于某种原因,IDE (1.8.19)没有从我放置文件的新位置读取文件,我必须删除旧文件夹,关闭IDE,然后才能更新文件,我在查看页面源代码时发现了这一点在浏览器中。
“innerHTML”属性:
“innerText”属性:
jQuery“text()”和“html()”方法:
https://linuxhint.com/change-label-text-javascript/
Apparently there are several ways to change the text of a label, I found this link page below, which has an explanation of each of these ways (it is a lot of text, so I attached only the examples here).
In my case none of them were working, even using the javascript code after uploading the tag, because I'm using ESP32 as a server, and Arduino IDE to upload the page, for some reason the IDE (1.8.19) was not reading the files from the new location I put the files, I had to delete the old folder, to close the IDE, and only then was it possible to update the files, I discovered this when looking at the source code of the page in the browser.
“innerHTML” property:
“innerText” property:
jQuery “text()” and “html()” methods:
https://linuxhint.com/change-label-text-javascript/
问题是页面未加载,因此元素未呈现。
所以你的代码找不到'lbltipAddedComment'
避免此类问题的最好方法是将你的代码包装
在使用jQuery.ready()之前,我们必须确保JQuery存在。
the issue is that the page was not loaded and therefor the elements not rendered .
So your code can not find 'lbltipAddedComment'
best way to avoid such issues is to wrap your code in
Before using jQuery.ready() ,we must ensure that the JQuery exist .