简体   繁体   English

如何在 javascript 中 select 一系列 html 表格单元格值?

[英]how to select a range of html table cell value in javascript?

Please help.请帮忙。 I have a html table and for example I want to select the first 4 cell value of the table but I don't know how: I tried.我有一个 html 表,例如我想 select 表的前 4 个单元格值,但我不知道如何:我试过了。 var table = document.getElementById("td") selectedCell = table:cells[0:4] var table = document.getElementById("td") selectedCell = table:cells[0:4]

javascript does not understand this way. javascript 不理解这种方式。

You can access a cell by its row index and cell index like this:您可以通过其行索引和单元格索引访问单元格,如下所示:

 var table = document.getElementById('table1') for(let rowIndex = 0; rowIndex < 2; rowIndex++){ for(let cellIndex = 0; cellIndex < 2; cellIndex++){ console.log(table.rows[rowIndex].cells[cellIndex]); } }
 <table id="table1" style="width:100%"> <caption>Monthly savings</caption> <tr> <th>Month</th> <th>Savings</th> </tr> <tr> <td>January</td> <td>$100</td> </tr> <tr> <td>February</td> <td>$50</td> </tr> <tr> <td>March</td> <td>$70</td> </tr> <tr> <td>April</td> <td>$160</td> </tr> </table>

if you give some sample data and an expected output I can use it to help you to make adjustment to this.如果您提供一些示例数据和预期的 output,我可以使用它来帮助您对此进行调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM