Debugging IE8 Javascript Replace innerHTML Runtime

2020-02-05 01:55发布

function DeleteData(ID)
{
 var ctrlId=ID.id;

 var divcontents=document.getElementById(ctrlId).innerHTML;
 var tabid=ctrlId.replace(/div/,'tab');
 var tabcontents=document.getElementById(tabid).innerHTML;
 alert(document.getElementById(tabid).innerHTML);
 document.getElementById(tabid).innerHTML="<TBody><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr></TBody>";
 document.getElementById(ctrlId).innerHTML='';

}

I am trying to replace the Table with empty table but

document.getElementById(tabid).innerHTML="<TBody><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr></TBody>";

this line is causing Unknown Runtime Error

10条回答
Emotional °昔
2楼-- · 2020-02-05 02:42

In IE8, you cannot change the innerHTML of an object when the code that attempts that is fired from within the object.

For example:

<span id='n1'>
  <input type=button value='test' onclick='DoSomething(this);'>
</span>

with the following code tucked in some remote corner of your document:

<script type='text/javascript'>
  function DoSomething(element)
  {
    document.getElementById("n1").innerHTML = "This is a test"
  }
</script>

This will fail with the error unknown runtime error. I believe it has to do with the fact that you're trying to replace the HTML code which fired the current line of execution. The onclick event is part of the code being replaced by the DoSomething function. IE8 doesn't like that.

I resolved my issue by setting a timer event for 250 milliseconds, such that the function called at the end of the 250 milliseconds replaces the span's innerHTML.

查看更多
Evening l夕情丶
3楼-- · 2020-02-05 02:46

I had the same issue but my error was because I was inserting a p tag directly underneath a p element as in:

document.getElementById('p-element').innerHTML = '<p>some description</p>';

I don't really see how this is HTML format error; seems more like another IE8 bug.

查看更多
爷、活的狠高调
4楼-- · 2020-02-05 02:47

If you need to set innerHTML to "" (empty string) then you can use removeChild instead

查看更多
够拽才男人
5楼-- · 2020-02-05 02:48

Great, I had the same situation with setting the innerHTML. When I read this I realised that one of the elements was a TR and one was a TD and the TD was working.

Changing the code so that they're both TDs fixes it, which means that it is a rather non-obvious problem caused by the structure of tables.

Presumably it throws the DOM awry when I start changing table rows since it can't store the table as an array any more.

I'm sure IE could give a more informative error message since it can't be that uncommon for the DOM to change on the fly and it seems strange that this would only throw an error for tables.

查看更多
登录 后发表回答