IE Vs FireFox中的Javascript错误?

时间:2012-06-20 08:43:07

标签: javascript internet-explorer firefox google-chrome

我使用以下脚本从HTML Table获取值。如果我使用 innerText ,它将适用于IE和Chrome Fine.But FireFox显示错误:row.cells [ 0] .innerText未定义源。如果我使用 textContent 它将在Chrome和FireFox中正常工作。但IE显示以下错误 cells.0.textContent'为null或不是对象。如何更改此脚本在 IE,Chrome,FireFox 上工作,没有错误?我使用c = row.cells [0] .innerText.strip(); c = row.cells [0] .textContent.strip();

        function a()
        {

            var table = getServerObject("b");
            var row,c;
            for (var i = 2; i < table.rows.length - 1; i++)
            {
                row = table.rows[i]; 
                if(row.cells.length==1)continue;
                c= row.cells[0].innerText.strip(); //It was work in chrome and IE (or)
                c=row.cells[0].textContent.strip();//It was work in chrome and FF
                if(c==0)
               {
                //Something
               }

            }
        }

3 个答案:

答案 0 :(得分:5)

在使用可用的属性之前进行测试:

var contentEnabled = document.textContent === null;

稍后您有if决定使用哪个属性

if ( contentEnabled ) {
  c= row.cells[0].textContent.strip(); // It was work in chrome and FF
} else {
  c= row.cells[0].innerText.strip(); // It was work in chrome and IE
}
@RobW建议

或更短

c = row.cells[0][contentEnabled ? 'textContent' : 'innerText'].strip();

对于两个属性之间的细微差别,请注意MDN docu on textContent中的以下内容:

  

与innerText的区别

     

Internet Explorer介绍了element.innerText。意图与几个不同之处几乎相同:

     

请注意,虽然textContent获取了所有元素的内容,包括<script><style>元素,但大多数等效的特定于IE的属性innerText却没有。       innerText也知道样式,不会返回隐藏元素的文本,而textContent会。       由于innerText知道CSS样式,它会触发重排,而textContent则不会。

答案 1 :(得分:0)

function a()
        {

            var table = getServerObject("b");
            var row,c;
            for (var i = 2; i < table.rows.length - 1; i++)
            {
                row = table.rows[i]; 
                if(row.cells.length==1)continue;
                   if(typeof (row.cells[0]) != "undefined"){
                c= row.cells[0].innerText.strip(); //It was work in chrome and IE (or)
                c=row.cells[0].textContent.strip();//It was work in chrome and FF
                if(c==0)
               {
                //Something
               }
                 }
            }
        }

答案 2 :(得分:0)

如果你真的想要一种跨浏览器的方式(IE&lt; 9),请使用jQuery。说真的,你会花很多时间来解决这些怪癖。

基于它的灵感,您可以像这样做:使用nodeValue,这是唯一的跨浏览器方式。但是,nodeValue不适用于元素,但它适用于textNodesfull list关于其工作的元素)。

function getText( el ) {
    var text = '';

    // Recursively get the text
    ( function recur( el ) {

        // If it's a textNode or a CDATA node
        if ( el.nodeType === 3 || el.nodeType === 4 ) {
            text += el.nodeValue;
        }

        // If it has childNodes, recursively get their nodeValue
        if ( el.hasChildNodes() ) {
            for ( var i = 0, l = el.childNodes; i < l; i++ ) {
                recur( el.childNodes[ i ] );
            }
        }
    } () );
    return text;
}

用法:

getText( row.cells[0] );

如果你不关心差异(innerTexttextContent不返回相同的输出,更不用说它得到了哪些元素,还有textNodes差异)并且只想要一个快速解决方案,使用此:

function getText( el ) {
    if ( 'textContent' in el ) return el.textContent;
    else return el.innerText;
}
相关问题