Javascript childNodes 不能在 Firefox 和 Chrome 中工作,但可以在 IE 中工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2154801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
childNodes not working in Firefox and Chrome but working in IE
提问by Muhammad Akhtar
I have a gridview in its 3rd cell, there is textbox control, I am calling javascript function on onchange.
我的第三个单元格中有一个 gridview,有文本框控件,我在 onchange 上调用 javascript 函数。
Can some body tell me why this is not working in Firefox and Chrome but working in IE
有人可以告诉我为什么这在 Firefox 和 Chrome 中不起作用,但在 IE 中起作用
grd.rows[rowindex].cells[3].childNodes[0].value
It return correct value in IE but not in Chrome and firefox (In FF and Chrome it return undefined)?
它在 IE 中返回正确的值,但在 Chrome 和 firefox ( In FF and Chrome it return undefined) 中却没有?
Please also suggest me solution to handle this problem.
还请建议我解决此问题的解决方案。
Edit
编辑
alert(grd.rows[ri].cells[3].childNodes[0].value);//IE value=correct value, FF and chrome value=undfined
alert(grd.rows[ri].cells[3].childNodes[1].value);//IE value=undfined, FF and Chrome value= correct value
Thanks
谢谢
采纳答案by ChaosPandion
I believe that this is because IE ignores text nodes that only contain newlines and tabs. Personally I prefer they be ignored but I would rather have consistency across the browsers.
我相信这是因为 IE 忽略了只包含换行符和制表符的文本节点。我个人更喜欢忽略它们,但我宁愿在浏览器之间保持一致性。
<p><!-- This comment represents a text node.
--><em>text</em>
</p>
回答by Falcon
try grd.rows[rowindex].cells[3].childNodes[1].value
试试 grd.rows[rowindex].cells[3].childNodes[1].value
or the best, look at table in integrated Developer tool
或者最好,查看集成开发人员工具中的表格
回答by Pawan Rahate
@ChaosPandion:
@ChaosPandion:
Hey friend don't use this type of check for childNodes.
嘿朋友不要对 childNodes 使用这种类型的检查。
The counting of childNodes varies. Some browsers include empty textNodes, some do not. In this sort of operation as I believe you are describing, it is better to use the parent's getElementsByTagName()method. That way the number of chidren and index of each child you are looking for will be consistent.
childNode 的计数各不相同。有些浏览器包含空的 textNode,有些则没有。在我相信您所描述的这种操作中,最好使用父级的getElementsByTagName()方法。这样,您正在寻找的每个孩子的孩子数量和索引将是一致的。
OR
或者
just check your browser's name.
只需检查浏览器的名称。
if it is IE then as it neglects empty textnode, the childNode in it is less by one number than other browsers.
如果是 IE 则因为它忽略了空的 textnode,所以其中的 childNode 比其他浏览器少一个数字。
for eg.
例如。
var isIE = navigator.appName;
if (isIE == "Microsoft Internet Explorer") {
var removeProductID = document.getElementById(obj.childNodes[0].id).getAttribute("abc");
}
else {
var removeProductID = document.getElementById(obj.childNodes[1].id).getAttribute("abc");
}
Hope this helps. Enjoy coding.
希望这可以帮助。享受编码。
回答by Ishwar Pohani
Try out this. I have same problem and this problem is resolved by just replace "childNodes" with "children"
试试这个。我有同样的问题,只需将“childNodes”替换为“children”即可解决此问题
alert(grd.rows[ri].cells[3].children[0].value);
回答by Cody Hackjob
Ishwar's answer of changing childNodes to children worked for me.
Ishwar 将 childNodes 更改为 children 的答案对我有用。
回答by Sergey Ilinsky
If you are looking for the text, use grd.rows[rowindex].cells[3].childNodes[0].data for non-IE browsers.
如果您正在查找文本,请对非 IE 浏览器使用 grd.rows[rowindex].cells[3].childNodes[0].data。
Getting text value of an Element Node
获取元素节点的文本值
var oCell = grd.rows[rowindex].cells[3];
alert(oCell.textContent || oCell.innerText)
Getting text value of a Text Node (less safe compared to previous)
获取文本节点的文本值(与以前相比不太安全)
var oText = grd.rows[rowindex].cells[3].childNodes[0];
alert(oCell.data || oCell.value)
回答by Sean Hogan
As ChaosPandion says, IE ignores white-space text-nodes. The following should work cross-browser:
正如 ChaosPandion 所说,IE 忽略空白文本节点。以下应该跨浏览器工作:
var cell = grd.rows[rowindex].cells[3];
for (var textbox=cell.firstChild; textbox.nodeType!==1; textbox=textbox.nextSibling);
alert(textbox.value);
However, you say you are calling the function on onchange. Presumably that means the onchange event for the textbox in question. In that case the event argument in your event handler should have a pointer to the textbox. Look at the target or srcElement property. e.g.
但是,您说您是在 onchange 上调用该函数。大概这意味着有问题的文本框的 onchange 事件。在这种情况下,事件处理程序中的事件参数应该有一个指向文本框的指针。查看 target 或 srcElement 属性。例如
function onChange(e) {
e = e || window.event;
var textbox = e.target || e.srcElement;
alert(textbox.value);
}
回答by ifti
try getElementsByTagName() instead of ChildNodes. it will be working for FF , chrome and for IE as well.
尝试 getElementsByTagName() 而不是 ChildNodes。它也适用于 FF、chrome 和 IE。

