Home  >  Article  >  Web Front-end  >  JS uses for loop to traverse all cell contents of Table_javascript skills

JS uses for loop to traverse all cell contents of Table_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:39:071945browse

The idea of ​​JS traversing the contents of all cells in the Table is to traverse all the Rows in the Table, traverse each column in the Row, and obtain the contents of the cells in the Table

function GetInfoFromTable(tableid) {
  var tableInfo = "";
  var tableObj = document.getElementById(tableid);
  for (var i = 0; i < tableObj.rows.length; i++) {  //遍历Table的所有Row
    for (var j = 0; j < tableObj.rows[i].cells.length; j++) {  //遍历Row中的每一列
      tableInfo += tableObj.rows[i].cells[j].innerText;  //获取Table中单元格的内容
      tableInfo += "  ";
    }
    tableInfo += "\n";
  }
  return tableInfo;
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn