childNodes not working in Firefox and Chrome but w

2019-02-14 10:35发布

I have a gridview in its 3rd cell, there is textbox control, I am calling javascript function on onchange.

Can some body tell me why this is not working in Firefox and Chrome but working in 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)?

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

9条回答
冷血范
2楼-- · 2019-02-14 11:34

@ChaosPandion:

Hey friend don't use this type of check for 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.

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.

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.

查看更多
时光不老,我们不散
3楼-- · 2019-02-14 11:37

Ishwar's answer of changing childNodes to children worked for me.

查看更多
▲ chillily
4楼-- · 2019-02-14 11:37

try getElementsByTagName() instead of ChildNodes. it will be working for FF , chrome and for IE as well.

查看更多
登录 后发表回答