簡體   English   中英

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

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

請幫忙。 我有一個 html 表,例如我想 select 表的前 4 個單元格值,但我不知道如何:我試過了。 var table = document.getElementById("td") selectedCell = table:cells[0:4]

javascript 不理解這種方式。

您可以通過其行索引和單元格索引訪問單元格,如下所示:

 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>

如果您提供一些示例數據和預期的 output,我可以使用它來幫助您對此進行調整。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM