Java 脚本 for 循环仅迭代表中的第一行

发布于 2025-01-11 03:56:13 字数 1789 浏览 0 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文