childNodes not working in Firefox and Chrome but working in IE

前端 未结 8 1941
太阳男子
太阳男子 2021-01-06 07:41

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 a

相关标签:
8条回答
  • 2021-01-06 07:57

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

    0 讨论(0)
  • 2021-01-06 08:00

    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)
    
    0 讨论(0)
  • 2021-01-06 08:01

    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>
    
    0 讨论(0)
  • 2021-01-06 08:04

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

    or the best, look at table in integrated Developer tool

    0 讨论(0)
  • 2021-01-06 08:08

    @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.

    0 讨论(0)
  • 2021-01-06 08:13

    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);
    
    0 讨论(0)
提交回复
热议问题