Java 脚本 for 循环仅迭代表中的第一行
我的 for 循环将迭代多次,但我想要对单元格进行的任何更改仅发生在第一行中。行和单元格的计数器都会上升,但唯一更新的单元格是第一行中的单元格。我什至通过控制台进行了检查,其他行没有发生任何事情。
注意:我只想更新具有 ID 的单元格
var table = document.getElementById("myTable");
for (var r = 0, n = table.rows.length; r < n; r++) //for every row
{
for (var c = 0, m = table.rows[r].cells.length; c < m; c++) //for every cell
{
var x = document.getElementsByTagName("td")[c];
console.log("row counter " + r);
console.log("cell counter " + c);
if (x.id == "wed_asso") {
schedule_wed();
x.innerHTML = wed_associate;
}
if (x.id == "asso") {
schedule();
x.innerHTML = associate;
}
}
}
<table id="myTable">
<tr>
<th> </th>
<th> Wednesday </th>
<th> Thursday</th>
<th> Friday</th>
<th> Saturday</th>
<th> Thursday</th>
<th> Friday</th>
<th> Saturday</th>
</tr>
<tr>
<td> TDR</td>
<td>John</td>
<td id="asso"></td>
<td>Alec</td>
<td id="asso"></td>
<td id="asso"></td>
<td>Alec</td>
<td id="asso"></td>
</tr>
<tr>
<td> </td>
<td id="wed_asso"> </td>
<td id="asso"> </td>
<td id="asso"> </td>
<td id="asso"> </td>
<td id="asso"> </td>
<td id="asso"> </td>
<td id="asso"> </td>
</tr>
</table>
My for loop will iterate multiple times but any changes I want to make to the cells only happens in the first row. The counters for the rows and the cells all go up but the only cells that are updated are the ones in the first row. I've even examined through the console and nothing is happening to the other rows.
Note: I only want the cells with the ID's to be updated
var table = document.getElementById("myTable");
for (var r = 0, n = table.rows.length; r < n; r++) //for every row
{
for (var c = 0, m = table.rows[r].cells.length; c < m; c++) //for every cell
{
var x = document.getElementsByTagName("td")[c];
console.log("row counter " + r);
console.log("cell counter " + c);
if (x.id == "wed_asso") {
schedule_wed();
x.innerHTML = wed_associate;
}
if (x.id == "asso") {
schedule();
x.innerHTML = associate;
}
}
}
<table id="myTable">
<tr>
<th> </th>
<th> Wednesday </th>
<th> Thursday</th>
<th> Friday</th>
<th> Saturday</th>
<th> Thursday</th>
<th> Friday</th>
<th> Saturday</th>
</tr>
<tr>
<td> TDR</td>
<td>John</td>
<td id="asso"></td>
<td>Alec</td>
<td id="asso"></td>
<td id="asso"></td>
<td>Alec</td>
<td id="asso"></td>
</tr>
<tr>
<td> </td>
<td id="wed_asso"> </td>
<td id="asso"> </td>
<td id="asso"> </td>
<td id="asso"> </td>
<td id="asso"> </td>
<td id="asso"> </td>
<td id="asso"> </td>
</tr>
</table>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论