Javascript IE与FireFox中的Java脚本错误?

Javascript IE与FireFox中的Java脚本错误?,javascript,internet-explorer,firefox,google-chrome,Javascript,Internet Explorer,Firefox,Google Chrome,我使用以下脚本从HTML表中获取值。如果我使用innerText,它将在IE和Chrome Fine上工作。但是FireFox显示错误:row.cells[0].innerText是未定义的源。如果我使用textContent,它在Chrome和FireFox中可以正常工作。但是IE显示以下错误单元格。0.textContent'为空或不是对象。如何在IE、Chrome、FireFox上更改此脚本而不出错?我使用c=row.cells[0].innerText.strip()中的任意一个或c=r

我使用以下脚本从HTML表中获取值。如果我使用innerText,它将在IE和Chrome Fine上工作。但是FireFox显示错误:row.cells[0].innerText是未定义的源。如果我使用textContent,它在Chrome和FireFox中可以正常工作。但是IE显示以下错误单元格。0.textContent'为空或不是对象。如何在IE、Chrome、FireFox上更改此脚本而不出错?我使用c=row.cells[0].innerText.strip()中的任意一个c=row.cells[0].textContent.strip()

函数a()
{
var table=getServerObject(“b”);
变量行,c;
对于(var i=2;i
函数a()
{
var table=getServerObject(“b”);
变量行,c;
对于(var i=2;i
在使用属性之前只需进行测试,该属性可用:

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();
对于两种属性之间的细微差异,请注意以下内容:

与内部文本的差异

Internet Explorer引入了
元素.innerText
。其意图大致相同,但存在一些差异:

请注意,虽然
textContent
获取所有元素的内容,包括
元素,但大部分等效的IE特定属性
innerText
不会。
innerText
也知道样式,不会返回隐藏元素的文本,而
textContent
会。 由于
innerText
知道CSS样式,它将触发回流,而
textContent
不会


如果你真的想要一种跨浏览器的方式(我读了同一页,上面说
document.textContent
是显式的
null
,如果支持的话,否则它是
未定义的
。我想你最好检查一下
document
中的
“textContent”。为了减少重复,我会使用
c=row.cells[0][contentEnabled?'textContent':'innerText'].strip();
。也
..==null?true:false
可以缩短为
..==null
@RobW编辑并包括在内。谢谢。
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
}
c = row.cells[0][contentEnabled ? 'textContent' : 'innerText'].strip();
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] );
function getText( el ) {
    if ( 'textContent' in el ) return el.textContent;
    else return el.innerText;
}