从表中删除所有子元素(第一个元素除外)? [英] Remove all child elements from table except for first?

查看:95
本文介绍了从表中删除所有子元素(第一个元素除外)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的HTML:

<table id="user-table">
      <tr><td>1</td></tr>
      <tr><td>2</td></tr>
      <tr><td>3</td></tr>     
</table>

如何使用jQuery删除用户表中除第一个<tr>之外的所有子元素?

How can I remove all child elements of the user-table except the first <tr> using jQuery?

我尝试过的事情:

$("#user-table").children().remove();

但是它删除了所有子<tr>元素,但我希望第一行不被删除.

However it removes all the child <tr> elements but I want the first row not to be removed.

到目前为止,这是我的jQuery:

Here's my jQuery so far:

$('#add-form').submit(function(e) {
    e.preventDefault();
    $.post("../lib/ajax/add-user.php", $("#add-form").serialize(),
    function(data){ 
    javascript:jQuery.fancybox.close();

    $("#user-table").children().remove();
    //$('#list-box').fadeTo(800,1,function(){

    //$(this).empty();
    //$(this)..append(data);                            

    //});
});

推荐答案

slice() 到目前为止 最快的方法 这个:

slice() is by far the fastest way you're going to be able to do this:

$('#user-table tr').slice(1).remove();

jsFiddle此处.

这篇关于从表中删除所有子元素(第一个元素除外)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆