从 HTML 表中读取数据的 JavaScript

我正在尝试使用 JavaScript 开发一个程序,该程序可以从 HTML 表中读取和编译数据。

最终目标是识别并显示每行中标记(填充)的字段数,然后将其显示为数字数据。

如果你能帮助我理解这一点,请这样做。

希望这能解释它,如果你有问题,我会回答。


繁花如伊
浏览 137回答 1
1回答

胡子哥哥

您可以使用 HTML Table id 检索您的表格,并对每个单元格值进行如下处理。您需要以整数或浮点数或任何您想应用于单元格值的数字格式将应用转换应用于单元格值。<table id="cTable">&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td>L1</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>L2</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>L3</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td>M1</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>M2</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>M3</td>&nbsp; &nbsp; </tr></table><script>&nbsp; &nbsp; //get the table using the Table id&nbsp; &nbsp; var mTable = document.getElementById('cTable');&nbsp; &nbsp; //get the number of rows of the table&nbsp; &nbsp; var rowLen = mTable.rows.length;&nbsp; &nbsp; //loop through rows&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; for (i = 0; i < rowLen; i++){&nbsp; &nbsp; &nbsp; //gets number of cells of current row&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;var rowCells = mTable.rows.item(i).cells;&nbsp; &nbsp; &nbsp; &nbsp;//get the amount of cells of current row&nbsp; &nbsp; &nbsp; &nbsp;var cellLen = rowCells.length;&nbsp; &nbsp; &nbsp; &nbsp;//loop through each cell in the current row&nbsp; &nbsp; &nbsp; &nbsp;for(var j = 0; j < cellLen; j++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // get your cell info here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var cellVal = rowCells.item(j).innerHTML;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // You can do the appropriate processing of numerical values below.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert(cellVal);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }</script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript