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();
});