删除表中除标题行外的所有行

使用 Javascript(使用 JQuery),我想删除表中除标题行之外的所有行。


这似乎是一件简单的事情,因为我在 StackOverFlow 上看到了很多关于此的帖子以及提供和接受的许多解决方案。但是,它们似乎都不适合我。请参考我下面的代码:


function delTable() {

  console.log("Delete all rows, but the header");


  // Option-A

  // $('#TableA tbody tr').remove();


  // Option-B

  // Accepted answer for: https://stackoverflow.com/questions/9420203/how-to-remove-all-rows-of-the-table-but-keep-the-header

  // $('#TableA tr').not(function(){ return !!$(this).has('th').length; }).remove();


  // Option-C

  $('#TableA tbody').empty();


}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<html>


<body onLoad="delTable();">

  <table id="TableA">

    <th>

      <tr>

        <td>Col A</td>

        <td>Col B</td>

      </tr>

    </th>

    <tbody>

      <tr>

        <td>1</td>

        <td>2</td>

      </tr>

      <tr>

        <td>3</td>

        <td>4</td>

      </tr>

    </tbody>

  </table>


</body>


</html>

有谁知道我做错了什么?谢谢。



MMMHUHU
浏览 126回答 4
4回答

DIEA

只需将您的 th 更新为 thead 就可以了,请参见下文:HTML:<!DOCTYPE html><html><head>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.js"></script>&nbsp;&nbsp;</head><body onLoad="delTable()">&nbsp; <table id="TableA">&nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td>Col A</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>Col B</td>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </thead>&nbsp; &nbsp; <tbody>&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td>1</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>2</td>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td>3</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>4</td>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </tbody>&nbsp; </table></body></html>JS:function delTable() {&nbsp; console.log("Delete all rows, but the header");&nbsp; // Option-A&nbsp; // $('#TableA tbody tr').remove();&nbsp; // Option-B&nbsp; // Accepted answer for: https://stackoverflow.com/questions/9420203/how-to-remove-all-rows-of-the-table-but-keep-the-header&nbsp; // $('#TableA tr').not(function(){ return !!$(this).has('th').length; }).remove();&nbsp; // Option-C&nbsp; $('#TableA tbody').empty();}工作小提琴: https ://jsfiddle.net/pvfkxdby/1/

梦里花落0921

这对我有用。<th>你用而不是分开你的标题,<thead>然后你<td>在你的标题中而不是<th>。$(document).ready(() => {&nbsp; $("#delete").click(() => {&nbsp; &nbsp; $("#TableA tbody tr").remove()&nbsp; })})<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><table id="TableA">&nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <th>Col A</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>Col B</th>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </thead>&nbsp; &nbsp; <tbody>&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td>1</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>2</td>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td>3</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>4</td>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </tbody>&nbsp;</table>&nbsp;<button id="delete">delete</button>

慕少森

通过在我的 Google Chrome 中检查您的 HTML,我可以看到行已重新排列,因此即使第一行也在<tbody>标签内。因此整个表被删除。您可以简单地通过将第一行包装在<thead>标签中或使用不使用<tbody>标签的不同方法来解决此问题。

手掌心

您需要替换th为thead并使用$('#TableA tbody').find("tr")如下:function delTable() {&nbsp; console.log("Delete all rows, but the header");&nbsp; $('#TableA tbody').find("tr").remove();}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><html><body onLoad="delTable();">&nbsp; <table id="TableA">&nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td>Col A</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>Col B</td>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </thead>&nbsp; &nbsp; <tbody>&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td>1</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>2</td>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td>3</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>4</td>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </tbody>&nbsp; </table></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript