使用Javascript更改标签文字 [英] Change label text using Javascript

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

问题描述

为什么这不适合我?

 < script> 
document.getElementById('lbltipAddedComment')。innerHTML ='您的提示已被提交!';
< / script>
< label id =lbltipAddedComment>< / label>


解决方案

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



这不起作用:

 < script> 
document.getElementById('lbltipAddedComment')。innerHTML ='您的提示已被提交!';
< / script>
< label id =lbltipAddedComment> test< / label>

这将起作用:

 < label id =lbltipAddedComment> test< / label> 
< script>
document.getElementById('lbltipAddedComment')。innerHTML ='您的提示已被提交!';
< / script>

这个例子(jsfiddle链接)维护订单(脚本第一,然后标签)并使用onLoad:

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



$ b addLoadEvent(function(){
document.getElementById('lbltipAddedComment')。innerHTML ='您的提示已被提交!';

});
< / script>


Why doesn't this work for me?

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

解决方案

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>

这篇关于使用Javascript更改标签文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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