Using jQuery how do I select a range of rows?

2020-01-29 02:24发布

Does a jQuery only solution exist for selecting a range of rows from a table?

I know 'eq', 'lt', 'gt' exist, but I'm looking for a combination of those selectors.

5条回答
姐就是有狂的资本
2楼-- · 2020-01-29 02:42

You could use the jQuery filter; one form of this takes a callback function as an argument — you can write any complex code in it for the selection.

查看更多
男人必须洒脱
3楼-- · 2020-01-29 02:43

You can chain your 'eq', 'lt', 'gt' and this will progressively filter each successive returned array.

查看更多
不美不萌又怎样
4楼-- · 2020-01-29 02:44

You can use the nthChild selector with an equation argument.

查看更多
混吃等死
5楼-- · 2020-01-29 03:00

Because :gt() is a jQuery extension... using :gt() cannot take advantage of the performance... For better performance in modern browsers, use $("css-selector").slice(index) instead.

Which says, it's better to use native array method slice to achieve the goal.

$('ul > li').slice(start, end).css("color", "blue")

Direct link: http://api.jquery.com/gt-selector/

查看更多
狗以群分
6楼-- · 2020-01-29 03:04

You can apply more than one filter at a time, although the 2nd filter applies to the results of the first, so the following would highlight starting from the 4th row (skips 0..2), and highlight for 3 rows (includes 0..2):

$('#t tr:gt(2):lt(3)').css('background-color', '#f00');
查看更多
登录 后发表回答