【问题标题】:Remove only the tr on the tbody仅删除 tbody 上的 tr
【发布时间】:2016-01-30 12:15:38
【问题描述】:

我有一个 html,其中当然有一个 table 元素,并且在 tbody 中,我有一个用于删除表格行的按钮。

我已经为按钮分配了onclick="" 函数。 (Function here)

如何只删除表体 (tbody) 中的行,而不删除表头 (thead) 中的行?

FIDDLE HERE

【问题讨论】:

标签: javascript html


【解决方案1】:

将正文行包装到 tbody 元素,然后您可以仅选择要删除的 table tbody tr。使用 jQuery 你可以做到$('table tbody tr').remove();

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="table">
  <thead>
    <tr>
      <th>Name</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Some content</td>
      <td>
        <button onClick="$(this).closest('tr').remove()">Remove</button>
      </td>
    </tr>

    <tr>
      <td>Some content</td>
      <td>
        <button onClick="$(this).closest('tr').remove()">Remove</button>
      </td>
    </tr>

    <tr>
      <td>Some content</td>
      <td>
        <button onClick="$(this).closest('tr').remove()">Remove</button>
      </td>
    </tr>

    <tr>
      <td>Some content</td>
      <td>
        <button onClick="$(this).closest('tr').remove()">Remove</button>
      </td>
    </tr>
  </tbody>
</table>
<button onClick="$('#table tbody tr').remove()">Remove all rows</button>

【讨论】:

    猜你喜欢
    • 2011-04-14
    • 1970-01-01
    • 2018-05-03
    • 2020-07-11
    • 2018-12-09
    • 2021-05-28
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多