问题
Possible Duplicate:
Selecting the first “n” items with jQuery
I have an infinite countable list of elements!
Is there a function to find x elements from the head of the list and stop!
$('.elements').find('li').limit(10) //this does not work
I tried the goog but i could not be specific enough with the ? !
回答1:
Yes, you can use jQuery's custom :lt selector for that.
For instance, this will find only the first three li
s in the given target list:
var firstThree = $("#target li:lt(3)");
Live Example | Source
回答2:
Try jQuery slice. Your code will be something like this:
$('.elements').find('li').slice(0,10);
UPDATE
As suggested by Marius Miliunas and T.J. Crowder, based on performance, you can just do this:
$('.elements li').slice(0,10);
来源:https://stackoverflow.com/questions/14005796/jquery-limit-element-query-results