Javascript 使用jQuery删除表中指定行之后的所有行

Javascript 使用jQuery删除表中指定行之后的所有行,javascript,jquery,html-table,Javascript,Jquery,Html Table,我想删除表中x行之后的所有行,其中x是一个向上切换的数字。以下操作不起作用: $("#comps-table tbody tr:gt(x)").remove(); 也没有: $('#comps-table tbody tr').gt(x).remove(); x是一个变量,因此需要使用字符串连接 $("#comps-table tbody tr:gt(" + x + ")").remove(); 或者首选的方法是使用slice() x是一个变量,因此需要使用字符串连接 $("#comps-

我想删除表中x行之后的所有行,其中x是一个向上切换的数字。以下操作不起作用:

$("#comps-table tbody tr:gt(x)").remove();
也没有:

$('#comps-table tbody tr').gt(x).remove();

x
是一个变量,因此需要使用字符串连接

$("#comps-table tbody tr:gt(" + x + ")").remove();
或者首选的方法是使用slice()


x
是一个变量,因此需要使用字符串连接

$("#comps-table tbody tr:gt(" + x + ")").remove();
或者首选的方法是使用slice()

试试这个:

var after = 5; // remove after row number 5

$('#comps-table tbody tr').each(function() {
    var $row = $(this);
    var rowNumber = $row.index();

    if ( rowNumber >= after ) {
         $row.remove();
    }
});
我还没有测试过它,但它应该能让你找到正确的方向

试试这个:

var after = 5; // remove after row number 5

$('#comps-table tbody tr').each(function() {
    var $row = $(this);
    var rowNumber = $row.index();

    if ( rowNumber >= after ) {
         $row.remove();
    }
});

我还没有测试过它,但它应该会让你走上正确的方向

没错,而且它感觉不到效率。阿伦·P·约翰尼的答案更合适——欢呼雀跃,而且感觉效率不高。阿伦·P·约翰尼的答案更合适——干杯