jQuery 如何删除表格的所有行但保留标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9420203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:46:58  来源:igfitidea点击:

How to remove all rows of the table but keep the header

jqueryasp.nethtml-table

提问by Marc

I want to remove all rows of my table except the header.

我想删除表格中除标题之外的所有行。

This is what I've tried but it always deletes all rows and header:

这是我尝试过的,但它总是删除所有行和标题:

$("#<%=tblDetailFourn.ClientID%> tbody tr").remove();

$("#<%=tblDetailFourn.ClientID%> tbody tr").not("thead tr").remove();

$("#<%=tblDetailFourn.ClientID%> tr").not("thead tr").remove();

$("#<%=tblDetailFourn.ClientID%> tbody").not("thead").remove();

$("#<%=tblDetailFourn.ClientID%> tbody").remove();

$("#<%=tblDetailFourn.ClientID%> > tbody").remove();

Here's the html:

这是html:

<table id="tblDetailFourn" runat="server" class="ProjetTable ProjetTableHover">
    <thead>
       <tr>
          <th style="width:200px">R?le de Ressource</th>
          <th style="width:200px">Nom Prénom</th>
          <th style="width:120px">Tel</th>
          <th style="width:200px">Courriel</th>
          <th style="width:80px">Actif</th>
          <th style="width:33px"></th>
          <th style="width:33px"></th>
      </tr>
    </thead>
    <tbody>
    </tbody>
</table>

采纳答案by Likwid_T

Try using this:

尝试使用这个:

$('#<%=tblDetailFourn.ClientID%> tr').not(function(){ return !!$(this).has('th').length; }).remove();

回答by Georg

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

回答by Brian Mains

Try http://api.jquery.com/child-selector/

试试http://api.jquery.com/child-selector/

$("#<%=tblDetailFourn.ClientID%> > tbody > tr").remove();

What you have should work though.

你所拥有的应该可以工作。

回答by SenorAmor

What about:

关于什么:

$('#tblDetailFourn tbody').html('');

$('#tblDetailFourn tbody').html('');

jsfiddle

提琴手

回答by jrummell

This should work, assuming that you don't have any header elements in tbody.

这应该有效,假设您在 tbody 中没有任何标题元素。

$("#<%=tblDetailFourn.ClientID%> tbody tr").remove();

回答by Michal Borek

Have you tried this?:

你试过这个吗?:

$("#<%=tblDetailFourn.ClientID%> tbody").html('')

回答by Md. Russel Hussain

Based on the html you provided the solution is following

根据您提供的 html,解决方案如下

$("#tblDetailFourn tbody").empty();

This will work perfectly.

这将完美地工作。

Thanks

谢谢

回答by GuestFirstPost

$('#tblDetailFourn > tbody > tr > td').parent('tr').empty();

回答by Sachin Gupta

if you want to delete all the tbody including the tag then use

如果要删除包括标签在内的所有 tbody,请使用

$("#tblDetailFourn tbody").remove();

it will remove all the tr under the tbody as well as tbody.

它将删除 tbody 和 tbody 下的所有 tr。