jQuery删除除第一个表外的所有表行 [英] jQuery delete all table rows except first

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

问题描述

使用jQuery,如何删除表中除第一行外的所有行?这是我第一次尝试使用索引选择器.如果我正确理解了这些示例,则应该可以进行以下操作:

Using jQuery, how do I delete all rows in a table except the first? This is my first attempt at using index selectors. If I understand the examples correctly, the following should work:

$(some table selector).remove("tr:gt(0)");

我将其读为在jQuery对象中包装一些表,然后删除此类行的元素索引大于零的所有'tr'元素(行)".实际上,它执行时不会产生错误,但是不会从表中删除任何行.

which I would read as "Wrap some table in a jQuery object, then remove all 'tr' elements (rows) where the element index of such rows is greater than zero". In reality, it executes without generating an error, but doesn't remove any rows from the table.

我缺少什么,该如何解决?当然,我可以使用简单的javascript,但是我对jQuery有很多乐趣,因此我想使用jQuery来解决.

What am I missing, and how do I fix this? Of course, I could use straight javascript, but I'm having so much fun with jQuery that I'd like to solve this using jQuery.

推荐答案

这应该有效:

$(document).ready(function() {
   $("someTableSelector").find("tr:gt(0)").remove();
});

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

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