Jquery limit element query results [duplicate]

拟墨画扇 提交于 2021-01-27 02:57:07

问题


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 lis 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!