2013-07-17 121 views
0

我试图通过使用innerText来提取texts没有html标记。如何在没有html标记的情况下提取文本?

//table is a html table structure 
var cells = table.innerText.trim() 

cells数据是这样的:

1st cell 


    2nd cell 


    3rd cell 


    4th cell 


    last cell 

我怎么每一个细胞的数据制作成array

我已经试过

cells = cells.split(' '); 

,但它不工作。

这里有什么帮助吗?非常感谢!

+0

'cells.replace( '\ n', '').replace( '\ r', '')分裂(”“);'可能工作 – Dementic

回答

4

只需提取从每个单元中的文本:

var arr=[], 
    rowCells, 
    myRows=table.rows; 
for (var i=0; i<myRows.length; i++) { 
    rowCells=myRows[i].cells; 
    for (var j=0; j<rowCells.length; j++) { 
     arr.push(rowCells[j].innerText.trim()); 
    } 
} 
+0

'arr.push(d.innerText || d.textContent);' – Dementic

+0

@Dementic我只是跟随OP谁想要使用innerText。顺便说一句,我更喜欢(d.textContent || d.innerText),因为铬。 – Christophe

+0

@Christophe当我console.log(table.cells)时,我得到了undefined .... + 1 – FlyingCat

相关问题