为了账号安全,请及时绑定邮箱和手机立即绑定

从 HTML 表中读取数据的 JavaScript

从 HTML 表中读取数据的 JavaScript

繁花如伊 2021-06-16 14:01:44
我正在尝试使用 JavaScript 开发一个程序,该程序可以从 HTML 表中读取和编译数据。最终目标是识别并显示每行中标记(填充)的字段数,然后将其显示为数字数据。如果你能帮助我理解这一点,请这样做。希望这能解释它,如果你有问题,我会回答。
查看完整描述

1 回答

?
胡子哥哥

TA贡献1825条经验 获得超6个赞

您可以使用 HTML Table id 检索您的表格,并对每个单元格值进行如下处理。您需要以整数或浮点数或任何您想应用于单元格值的数字格式将应用转换应用于单元格值。


<table id="cTable">

    <tr>

        <td>L1</td>

        <td>L2</td>

        <td>L3</td>

    </tr>

    <tr>

        <td>M1</td>

        <td>M2</td>

        <td>M3</td>

    </tr>

</table>


<script>

    //get the table using the Table id

    var mTable = document.getElementById('cTable');


    //get the number of rows of the table

    var rowLen = mTable.rows.length;


    //loop through rows    

    for (i = 0; i < rowLen; i++){

      //gets number of cells of current row  

       var rowCells = mTable.rows.item(i).cells;


       //get the amount of cells of current row

       var cellLen = rowCells.length;


       //loop through each cell in the current row

       for(var j = 0; j < cellLen; j++){


              // get your cell info here

              var cellVal = rowCells.item(j).innerHTML;


              // You can do the appropriate processing of numerical values below.

              alert(cellVal); 

           }

    }

</script>


查看完整回答
反对 回复 2021-06-18
  • 1 回答
  • 0 关注
  • 136 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信