简体   繁体   English

根据其他父级删除另一个父级中的表格行元素

[英]remove table row element in another parent depending on other parent

Before commenting this as duplicate please read clearly.在将其评论为重复之前,请仔细阅读。

I have a table structure that looks like this:我有一个看起来像这样的表结构:

          <td>
              <table class="schedule_day_table">
                  <tr>
                      <td>1.</td>
                      <td><div>GO TO SCHOOL</div></td>
                  </tr>
                  <tr>
                      <td>.</td>
                      <td><div></div></td>
                  </tr>
                  <tr>
                      <td>3.</td>
                      <td><div>READ A BOOK</div></td>
                  </tr>
              </table>
          </td>

          <td>
              <table class="schedule_time_table">
                  <tr><td>08:30 - 09:15</td></tr>
                  <tr><td>09:25 - 10:10</td></tr>
                  <tr><td>11:30 - 12:15</td></tr>
              </table>
          </td>

As you can see I have schedule that prints out schedule_day_table the 1 and 3, but never prints out 2 because there is no schedule at that time, but schedule_time_table always prints out all times.如您所见,我有时间表打印出 schedule_day_table 1 和 3,但从不打印出 2,因为当时没有时间表,但 schedule_time_table 始终打印出所有时间。

What i am trying to achieve is I want to remove schedule_time_table tr element if there is only dot (.) in that schedule_day_table tr td i want to get output like this:我想要实现的是,如果 schedule_day_table tr td 中只有点 (.),我想删除 schedule_time_table tr 元素,我想像这样得到 output:

<td>
              <table class="schedule_day_table">
                  <tr>
                      <td>1.</td>
                      <td><div>GO TO SCHOOL</div></td>
                  </tr>
                  <tr>
                      <td>3.</td>
                      <td><div>READ A BOOK</div></td>
                  </tr>
              </table>
          </td>

          <td>
              <table class="schedule_time_table">
                  <tr><td>08:30 - 09:15</td></tr>
                  <tr><td>11:30 - 12:15</td></tr>
              </table>
          </td>

This must be done in JS in my case.在我的情况下,这必须在 JS 中完成。

This should work if you hook it on document load.如果您将它挂在文档加载上,这应该可以工作。

 const dayRows = document.querySelectorAll('.schedule_day_table tr'); const timeRows = document.querySelectorAll('.schedule_time_table tr'); dayRows.forEach((row, i) => { //select first cell of the row let td = row.querySelector('td'); if(td.innerHTML == ".") { row.parentNode.removeChild(row); timeRows[i].parentNode.removeChild(timeRows[i]); } });
 <td> <table class="schedule_day_table"> <tr> <td>1.</td> <td><div>GO TO SCHOOL</div></td> </tr> <tr> <td>.</td> <td><div></div></td> </tr> <tr> <td>3.</td> <td><div>READ A BOOK</div></td> </tr> </table> </td> <td> <table class="schedule_time_table"> <tr><td>08:30 - 09:15</td></tr> <tr><td>09:25 - 10:10</td></tr> <tr><td>11:30 - 12:15</td></tr> </table> </td>

        const scheduleDayTableRows = Array.from(document.querySelectorAll(".schedule_day_table tr"));

        scheduleDayTableRows.map((row) => {
            if (row.querySelector("td").innerHTML === ".") {
                row.remove();
            }
        })

How does it work?它是如何工作的?

First you need to select all table rows from 1st table, then you should iterate from these rows and find first nested td.首先,您需要 select 第一个表中的所有表行,然后您应该从这些行中迭代并找到第一个嵌套的 td。 If it's innerHTML is equal to "."如果它的innerHTML 等于“。” you can remove this row with row.remove()您可以使用 row.remove() 删除此行

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

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