简体   繁体   English

IE8标签通过JavaScript问题更新

[英]IE8 label update via javascript issue

I am trying to use javascript to update the text in a html label. 我正在尝试使用javascript更新html标签中的文本。

It works in all browsers except IE8. 它适用于除IE8之外的所有浏览器。 In IE8 the label appears to be updated but is not displayed on the screen. 在IE8中,标签似乎已更新,但未显示在屏幕上。

I've created demo code below that shows the issue. 我在下面创建了演示代码来演示问题。

Thanks 谢谢

<html>
 <head>
 <script>
  function sendRequest() {               
    document.getElementById('errormessage').textContent="test";
    alert("textContent : "+document.getElementById('errormessage').textContent);
  }   
</script>

</head>

<body>

  <a href="javascript:void(0)" onclick="sendRequest();"> Click me</a>
   <br/>
  <label id="errormessage" style="color:#F00">&nbsp;</label>
</body> 
</html>
document.getElementById('errormessage').innerHTML="test";

IE 8 and under doesn't have textContent. IE 8及以下版本没有textContent。

Try this: 尝试这个:

function setText(el, text){
    if(typeof el.innerText !== 'undefined')
        el.innerText = text;
    else
        el.textContent = text;
}

function getText(el){
    return el.innerText || el.textContent;
}

function sendRequest() {
    var el = document.getElementById('errormessage');
    setText(el, "test");
    alert("textContent : "+getText(el));
}   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM