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:13

try grd.rows[rowindex].cells[3].childNodes[1].value

or the best, look at table in integrated Developer tool

查看更多
虎瘦雄心在
3楼-- · 2019-02-14 11:20

If you are looking for the text, use grd.rows[rowindex].cells[3].childNodes[0].data for non-IE browsers.

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)
查看更多
我只想做你的唯一
4楼-- · 2019-02-14 11:20

Use children[0] instead of ChildNode as below:

var oText = grd.rows[rowindex].cells[3].children[0];
查看更多
看我几分像从前
5楼-- · 2019-02-14 11:25

As ChaosPandion says, IE ignores white-space text-nodes. The following should work cross-browser:

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.

function onChange(e) {
 e = e || window.event;
 var textbox = e.target || e.srcElement;
 alert(textbox.value);
}
查看更多
Luminary・发光体
6楼-- · 2019-02-14 11:26

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.

<p><!-- This comment represents a text node.
    --><em>text</em>
</p>
查看更多
唯我独甜
7楼-- · 2019-02-14 11:31

Try out this. I have same problem and this problem is resolved by just replace "childNodes" with "children"

alert(grd.rows[ri].cells[3].children[0].value);
查看更多
登录 后发表回答