IE8 label update via javascript issue

折月煮酒 提交于 2019-12-22 00:07:39

问题


I am trying to use javascript to update the text in a html label.

It works in all browsers except IE8. In IE8 the label appears to be updated but is not displayed on the screen.

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>

回答1:


IE 8 and under doesn't have 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));
}   



回答2:


document.getElementById('errormessage').innerHTML="test";


来源:https://stackoverflow.com/questions/9370184/ie8-label-update-via-javascript-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!