2

我使用以下脚本从 HTML 表中获取值。如果我使用 innerText,它将在 IE 和 Chrome Fine 上运行。但 FireFox 显示错误:row.cells[0].innerText is undefined Source。如果我使用textContent它将在 Chrome 和 FireFox 中正常工作。但 IE 显示以下错误cells.0.textContent' 为空或不是对象。如何更改此脚本在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
               }

            }
        }
4

3 回答 3

5

只需在使用该属性之前进行测试,该属性可用:

var contentEnabled = document.textContent === null;

稍后您可以决定是否使用哪个属性

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 文档中的以下内容textContent

与 innerText 的区别

Internet Explorer 推出element.innerText。意图几乎相同,但有几个不同之处:

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

于 2012-06-20T08:48:51.697 回答
0

If you truly want a cross-browser way (IE<9), use jQuery. Seriously, you will spend a lot less time with these quirks.

Based on its inspiration, you can do it like it does: use nodeValue, the only cross-browser way. However, nodeValue doesn't work on elements, but it does work on textNodes (full list on which elements it works).

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;
}

Usage:

getText( row.cells[0] );

If you don't care about the differences (innerText and textContent don't return the same output, not to mention which elements it gets, there are also textNodes differences) and just want a quick solution, use this:

function getText( el ) {
    if ( 'textContent' in el ) return el.textContent;
    else return el.innerText;
}
于 2012-06-20T09:10:31.137 回答
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
               }
                 }
            }
        }
于 2012-06-20T08:46:51.517 回答