调试 IE8 Javascript 替换innerHTML 运行时错误

发布于 2024-07-13 08:35:32 字数 807 浏览 4 评论 0原文

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='';

}

我正在尝试用空表替换表,但

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

此行导致未知运行时错误

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(10

疧_╮線 2024-07-20 08:35:41

如果您需要将innerHTML设置为“”(空字符串),那么您可以使用removeChild代替

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

迷鸟归林 2024-07-20 08:35:41

这不是一个大问题,因为几天前我遇到了同样的问题以及这个错误的原因
ie 中出现的情况是 - 我们的 html 格式存在错误,或者您正在使用 以外的元素来替换 innerHTML ie 仅使用,不要使用表格、div 来替换 innerHTML。

  • 斯瓦普尼尔克

This is not a Big issue, since i faced same problem few days ago and the reason this error
occurs in ie is that - There exists an error in our html format or you are using an element other than <td> to replace innerHTML ie use only, dont use tables,divs to replace innerHTML.

  • SwapnilK
清音悠歌 2024-07-20 08:35:40

为什么它发生在 IE 中...

案例场景:

在 Div 中访问 Div 以添加 ajax 内容时,IE 中出现 Javascript 未知运行时错误。

解决方案:

永远不要将嵌套的 div 放置在表单标签之间来播放其中的内容..:)

why it happend in IE...

case scenario:

Javascript unknown runtime error in IE while accessing Div inside Div to add ajax contents.

solution:

dont ever place nested divs in between the form tags to play with there contents .. :)

無心 2024-07-20 08:35:40

使用 YUI 时尝试使用

Y.one('#'+data.id).setContent(html); 

Y.one('#'+data.id).set('innerHTML' , html);

When using YUI try to use

Y.one('#'+data.id).setContent(html); 

instead of:

Y.one('#'+data.id).set('innerHTML' , html);
送君千里 2024-07-20 08:35:39

太好了,我在设置 innerHTML 时遇到了同样的情况。 当我读到这篇文章时,我意识到其中一个元素是 TR,另一个元素是 TD,并且 TD 正在工作。

更改代码使它们都是 TD 可以修复此问题,这意味着这是由表结构引起的相当不明显的问题。

当我开始更改表行时,大概它会导致 DOM 出错,因为它无法再将表存储为数组。

我确信 IE 可以提供更丰富的错误消息,因为 DOM 动态更改并不罕见,而且这只会引发表错误,这似乎很奇怪。

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.

美人如玉 2024-07-20 08:35:39

哦,是的 - 请记住 HTML 结构必须有效。

我尝试将整个页面加载到

标记中 - 它失败了(显然),将其更改为

标记解决了我的问题!

所以请记住 HTML 结构!

Ohh yes - remember that the HTML structure has to be valid.

I tried loading an entire page into a <p> tag - it failed (obviously), changing it to a <div> tag solved my problem!

So remember the HTML structure!

给妤﹃绝世温柔 2024-07-20 08:35:38

我发现在 IE8 中有些元素是只读的:COL、COLGROUP、FRAMESET、HEAD、HTML、STYLE、TABLE、TBODY、TFOOT、THEAD、TITLE、TR。

因此,如果您尝试为这些元素设置innerHTML,IE8 会通知 alter 并显示未知运行时错误。

更多详细信息请参见:http://msdn.microsoft.com/en-us/库/ms533897%28VS.85%29.aspx

最简单的方法是将只读元素转换为 div 元素。

I find out that in IE8 some elements are in readonly: COL, COLGROUP, FRAMESET, HEAD, HTML, STYLE, TABLE, TBODY, TFOOT, THEAD, TITLE, TR.

Therefore if you try to set innerHTML for these elements IE8 notify alter with Unknown Runtime Error.

More details here: http://msdn.microsoft.com/en-us/library/ms533897%28VS.85%29.aspx.

The simplest way is to convert read-only elements to div element.

櫻之舞 2024-07-20 08:35:38

我遇到了同样的问题,但我的错误是因为我直接在 p 元素下方插入 p 标签,如下所示:

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

我真的不明白这是 HTML 格式错误; 看起来更像是另一个 IE8 bug。

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.

赠意 2024-07-20 08:35:37

您无法将值设置为表的innerHTML,您应该访问子单元格或行并像这样更改它们:

document.getElementById(tabid).rows[0].cells.innerHTML = 'blah blah';

有关更多信息/示例:表、TableHeader、TableRow、TableData 对象

You can't set value to a table's innerHTML, you should access to child cells or rows and change them like that :

document.getElementById(tabid).rows[0].cells.innerHTML = 'blah blah';

For more info/example : Table, TableHeader, TableRow, TableData Objects

紙鸢 2024-07-20 08:35:37

在 IE8 中,当尝试从对象内部触发代码时,您无法更改对象的innerHTML。

例如:

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

将以下代码隐藏在文档的某个偏远角落:

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

这将失败,并出现错误未知运行时错误。 我相信这与您试图替换触发当前执行行的 HTML 代码有关。 onclick 事件是被 DoSomething 函数替换的代码的一部分。 IE8 不喜欢这样。

我通过将计时器事件设置为 250 毫秒解决了我的问题,这样在 250 毫秒结束时调用的函数就会替换跨度的innerHTML。

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.

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