使用 JavaScript 更改标签文本

发布于 2024-10-08 20:49:27 字数 222 浏览 0 评论 0原文

为什么以下对我不起作用?

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

ぇ气 2024-10-15 20:49:27

因为您的脚本在标签存在于页面(在 DOM 中)之前运行。将脚本放在标签后面,或者等到文档完全加载(使用 OnLoad 函数,例如 <代码>jQuery Ready()http://www.webreference .com/programming/javascript/onloads/

这不起作用:

<script>
  document.getElementById('lbltipAddedComment').innerHTML = 'your tip has been submitted!';
</script>
<label id="lbltipAddedComment">test</label>

这将起作用:

<label id="lbltipAddedComment">test</label>
<script>
  document.getElementById('lbltipAddedComment').innerHTML = 'your tip has been submitted!';
</script>

这个示例(jsfiddle link) 保持顺序(首先是脚本,然后是标签)并使用 onLoad:

<label id="lbltipAddedComment">test</label>
<script>
function addLoadEvent(func) {  
      var oldonload = window.onload;  
      if (typeof window.onload != 'function') {  
        window.onload = func;  
      } else {  
        window.onload = function() {  
          if (oldonload) {  
            oldonload();  
          }  
          func();  
        }  
      }  
    }  

   addLoadEvent(function() {  
document.getElementById('lbltipAddedComment').innerHTML = 'your tip has been submitted!';

    });  
</script>

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:

<script>
  document.getElementById('lbltipAddedComment').innerHTML = 'your tip has been submitted!';
</script>
<label id="lbltipAddedComment">test</label>

This will work:

<label id="lbltipAddedComment">test</label>
<script>
  document.getElementById('lbltipAddedComment').innerHTML = 'your tip has been submitted!';
</script>

This example (jsfiddle link) maintains the order (script first, then label) and uses an onLoad:

<label id="lbltipAddedComment">test</label>
<script>
function addLoadEvent(func) {  
      var oldonload = window.onload;  
      if (typeof window.onload != 'function') {  
        window.onload = func;  
      } else {  
        window.onload = function() {  
          if (oldonload) {  
            oldonload();  
          }  
          func();  
        }  
      }  
    }  

   addLoadEvent(function() {  
document.getElementById('lbltipAddedComment').innerHTML = 'your tip has been submitted!';

    });  
</script>
中二柚 2024-10-15 20:49:27

您是否尝试过 .innerText.value 而不是 .innerHTML

Have you tried .innerText or .value instead of .innerHTML?

匿名的好友 2024-10-15 20:49:27

因为执行脚本时并没有加载标签元素。交换标签和脚本元素,它就会起作用:

<label id="lbltipAddedComment"></label>
<script>
    document.getElementById('lbltipAddedComment').innerHTML = 'Your tip has been submitted!';
</script>

Because a label element is not loaded when a script is executed. Swap the label and script elements, and it will work:

<label id="lbltipAddedComment"></label>
<script>
    document.getElementById('lbltipAddedComment').innerHTML = 'Your tip has been submitted!';
</script>
拥抱影子 2024-10-15 20:49:27

这是使用 jQuery 更改标签文本的另一种方法:

<script>
  $("#lbltipAddedComment").text("your tip has been submitted!");
</script>

查看 JsFiddle 示例

Here is another way to change the text of a label using jQuery:

<script>
  $("#lbltipAddedComment").text("your tip has been submitted!");
</script>

Check the JsFiddle example

望她远 2024-10-15 20:49:27

请改用 .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?

会傲 2024-10-15 20:49:27

使用 .innerText 应该可以。

document.getElementById('lbltipAddedComment').innerText = 'your tip has been submitted!';

Using .innerText should work.

document.getElementById('lbltipAddedComment').innerText = 'your tip has been submitted!';
谜泪 2024-10-15 20:49:27

试试这个:

<label id="lbltipAddedComment"></label>
<script type="text/javascript"> 
      document.getElementById('<%= lbltipAddedComment.ClientID %>').innerHTML = 'your tip has been submitted!';
</script>

Try this:

<label id="lbltipAddedComment"></label>
<script type="text/javascript"> 
      document.getElementById('<%= lbltipAddedComment.ClientID %>').innerHTML = 'your tip has been submitted!';
</script>
小瓶盖 2024-10-15 20:49:27

显然有几种方法可以更改标签的文本,我在下面找到了这个链接页面,其中对每种方法都有解释(文本很多,所以我在这里只附加了示例)。

就我而言,即使在上传标签后使用 javascript 代码,它们也不起作用,因为我使用 ESP32 作为服务器,并使用 Arduino IDE 上传页面,出于某种原因,IDE (1.8.19)没有从我放置文件的新位置读取文件,我必须删除旧文件夹,关闭IDE,然后才能更新文件,我在查看页面源代码时发现了这一点在浏览器中。

如何使用 JavaScript 更改标签文本?

可以使用以下方法来更改标签文本
JavaScript:

“innerHTML”属性。
    “innerText”属性。
    jQuery“text()”和“html()”方法。

“innerHTML”属性:

<前><代码><中心><正文>
<标签 id = "lbl">DOM;


函数标签文本(){
让 get = document.getElementById('lbl')
get.innerHTML= "缩写名称为文档对象模型";
}

“innerText”属性:

<前><代码><中心><正文>
输入名称:

<标签 id="lbl">不适用

函数标签文本() {
让 get = document.getElementById('lbl');
让 name = document.getElementById('name').value;
get.innerText = 名称;
}

jQuery“text()”和“html()”方法:

<脚本 src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
<中心><主体>



<标签 id = "lbl2">内容:

函数标签文本() { $('#lbl1').text("Linuxhint") } 函数 labelText2() { $('#lbl2').html("JavaScript") }

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.

How to Change Label Text Using JavaScript?

The following approaches can be utilized to change label text in
JavaScript:

    “innerHTML” property.
    “innerText” property.
    jQuery “text()” and “html()” methods.

“innerHTML” property:

<center><body>
<label id = "lbl">DOM</label>
<br><br>
<button onclick= "labelText()">Click Here</button>
</body></center>

function labelText(){
 let get = document.getElementById('lbl')
 get.innerHTML= "The abbreviated name is Document Object Model";
}

“innerText” property:

<center><body>
 Enter a Name: <input type= "text" id= "name" value= "" autocomplete= "off">
<p><input type= "button" id= "bt" value= "Change Label Text" onclick= "labelText()"></p>
<label id="lbl">N/A</label>
</body></center>

function labelText() {
 let get = document.getElementById('lbl');
 let name = document.getElementById('name').value;
 get.innerText = name;
}

jQuery “text()” and “html()” methods:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<center><body>
<label id = "lbl1">This is the following Website:</label>
<br><br>
<label id = "lbl2">Content:</label>
<br><br>
<button onclick= "labelText()">Click for Website</button>
<button onclick= "labelText2()">Click for Content</button>
</body></center>

function labelText() {
 $('#lbl1').text("Linuxhint")
}
function labelText2() {
 $('#lbl2').html("JavaScript")
}

https://linuxhint.com/change-label-text-javascript/

夏日落 2024-10-15 20:49:27

问题是页面未加载,因此元素未呈现。
所以你的代码找不到'lbltipAddedComment'

<label id="lbltipAddedComment"></label>

避免此类问题的最好方法是将你的代码包装

document.onload(){}  or jQuery.ready(){} functions

在使用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'

<label id="lbltipAddedComment"></label>

best way to avoid such issues is to wrap your code in

document.onload(){}  or jQuery.ready(){} functions

Before using jQuery.ready() ,we must ensure that the JQuery exist .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文