调试 IE8 Javascript 替换innerHTML 运行时错误
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
如果您需要将innerHTML设置为“”(空字符串),那么您可以使用removeChild代替
If you need to set innerHTML to "" (empty string) then you can use removeChild instead
这不是一个大问题,因为几天前我遇到了同样的问题以及这个错误的原因
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.为什么它发生在 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 .. :)
使用 YUI 时尝试使用
:
When using YUI try to use
instead of:
太好了,我在设置
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 aTR
and one was aTD
and theTD
was working.Changing the code so that they're both
TD
s 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.
哦,是的 - 请记住 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!
我发现在 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.
我遇到了同样的问题,但我的错误是因为我直接在
p
元素下方插入p
标签,如下所示:我真的不明白这是 HTML 格式错误; 看起来更像是另一个 IE8 bug。
I had the same issue but my error was because I was inserting a
p
tag directly underneath ap
element as in:I don't really see how this is HTML format error; seems more like another IE8 bug.
您无法将值设置为表的innerHTML,您应该访问子单元格或行并像这样更改它们:
有关更多信息/示例:表、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 :
For more info/example : Table, TableHeader, TableRow, TableData Objects
在 IE8 中,当尝试从对象内部触发代码时,您无法更改对象的innerHTML。
例如:
将以下代码隐藏在文档的某个偏远角落:
这将失败,并出现错误未知运行时错误。 我相信这与您试图替换触发当前执行行的 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:
with the following code tucked in some remote corner of your document:
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.