[英]childNodes[] not working in IE9 as in IE7 and 8
I have some code that works in IE7 and 8 but not in 9 我有一些代码适用于IE7和8但不适用于9
var table = document.getElementById('RadGrid_ctl01').childNodes[2];
which doesn't work in IE9, now I did read that IE9 count white spaces etc and therefore the index won't be the same as in IE7 and IE8 so I debugged and found same values when I changed index from 2 to 4 like so: 这在IE9中不起作用,现在我确实读过IE9计数空格等,因此索引与IE7和IE8中的索引不一样,所以当我将索引从2改为4时我调试并找到了相同的值:
var table = document.getElementById('RadGrid_ctl01').childNodes[4];
However when I later on try to access the table object with this code 但是当我稍后尝试使用此代码访问表对象时
var editor = table.childNodes[i].childNodes[j].childeNodes[0]
the variable editor will get the expected value in IE7 and IE8, but in IE9 it becomes null since the childNodes[j] doesn't have any children. 变量编辑器将在IE7和IE8中获得预期值,但在IE9中它变为null,因为childNodes [j]没有任何子节点。 I have no idea what causes this.
我不知道是什么原因引起的。 both i and j starts at 0.
i和j都从0开始。
Anyone know what I am doing wrong? 谁知道我做错了什么?
The behaviour of IE9 is right, lower versions are wrong. IE9的行为是正确的,较低版本是错误的。 The problem is caused by whitespace between two html-tags, which should lead into text-nodes.
这个问题是由两个html标签之间的空白引起的,这些标签应该导致文本节点。
UPDATE: Added example 更新:添加了示例
<ul><li>Foo</li><li>Bar</li></ul>
<ul>
<li>Foo</li>
<li>Bar</li>
</ul>
Looks pretty much the same, doesn't it? 看起来几乎一样,不是吗? However the resulting DOM differs.
然而,由此产生的DOM不同。 First example would result in:
第一个例子会导致:
- ul
-- li
--- (text)Foo
-- li
---(text)Bar
While the second Markup leads into this: 而第二个Markup导致这个:
- ul
-- (text)
-- li
--- (text)Foo
-- (text)
-- li
--- (text)Bar
-- (text)
And since text-nodes cannot have any child-nodes, this causes your Javascript to fail silently. 由于文本节点不能有任何子节点,这会导致您的Javascript无声地失败。
Possible solution 可能解决方案
One solution is to strip out the empty text-nodes. 一种解决方案是去除空文本节点。 I once wrote a gist for this issue.
我曾经写过一个关于这个问题的要点 。
UPDATE: 更新:
Node.normalize()
seems to be a more reliable solution, but i didn't check it for browser-compatibilty. Node.normalize()
似乎是一个更可靠的解决方案,但我没有检查它的浏览器兼容性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.