Javascript 如何在不使用jQuery的情况下获取标记中的文本

Javascript 如何在不使用jQuery的情况下获取标记中的文本,javascript,firefox,cross-browser,Javascript,Firefox,Cross Browser,我正在看本页上的一个示例,其中有人演示了如何使用JavaScript从表中获取文本: var refTab=document.getElementById("ddReferences") var ttl; // Loop through all rows and columns of the table and popup alert with the value // /content of each cell. for ( var i = 0; i<refTab.rows.l

我正在看本页上的一个示例,其中有人演示了如何使用JavaScript从表中获取文本:

var refTab=document.getElementById("ddReferences") 
var  ttl; 
// Loop through all rows and columns of the table and popup alert with the value 
// /content of each cell. 
for ( var i = 0; i<refTab.rows.length; i++ ) { 
  var row = refTab.rows.item(i); 
  for ( var j = 0; j<row.cells.length; j++ ) { 
    var col = row.cells.item(j);
    alert(col.firstChild.innerText); 
  } 
}
当我在我试图创建的网页中尝试它时,它不起作用。(是的,我知道一个叫做JQuery的东西。我知道有人会告诉我去学。也许有一天我会学。)

问题是,这是一个非标准的MS属性,在某些浏览器上不受支持

该标准大致相当

然后,以下各项应起作用:

document.getElementById("ddReferences")
    .rows.item(0)
        .cells.item(0)
            .firstChild
                .textContent;
你在使用FireFox吗?(见橙色的“有用的回答”)

链接的Mozilla Support论坛帖子及其引用的博客帖子都建议使用textContent代替innerText。虽然这对一行简单的文本很好,但要注意它们是不同的textContent返回在源中写入的元素内的文本(忽略标记),而innerText返回在页面上呈现的文本

例如,使用以下HTML


富吧
两者

document.getElementById(“ddReferences”).rows.item(0).cells.item(0).firstChild.innerText

document.getElementById(“ddReferences”).rows.item(0).cells.item(0).firstChild.textContent
给你:

foo-bar

但是,使用此HTML:


foo
酒吧
innerText为您提供:

foo
酒吧
textContent为您提供:

foobar

…使用此HTML:


福
酒吧
innerText为您提供

foo-bar
textContent为您提供

foo
酒吧
用提琴做一些实验,你就会看到不同之处。

效果很好:实际上,我可以重现这个问题。它可以在Chrome和IE中使用,但在FireFox中不起作用,我想这就是OP所使用的。我试着发布一个答案,但我不能,因为这个问题被错误地关闭,认为是不可复制的。提名重新开放。
document.getElementById("ddReferences")
    .rows.item(0)
        .cells.item(0)
            .firstChild
                .textContent;