开发者

How to select all element leaving first element?

I have jquery selector like

$("li a")

how can I modify it so it select all e开发者_Go百科lements leaving first anchor tag ?


.not is used to exclude elements and the pseudo-selector :first is used to get the first one. You can combine both to exclude first element like below:

$("li a").not(":first");

Or you can use the :not pseudo-selector:

$("li a:not(:first)");

Update

I have profiled three different methods and it appears that using the .not method along with :first selector is faster (tested in Chrome 14).

And another update, after reading @Jon's comment from the jQuery docs... using .slice is significantly faster than all jQuery methods! Here's a screenshot of the test results on Chrome 14:

How to select all element leaving first element?


You can use the :gt selector:

$("li a:gt(0)")

Update: If you are interested in performance, the fastest by far is

$("li a").slice(1)

about which the docs say:

Because :gt() is a jQuery extension and not part of the CSS specification, queries using :gt() cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. For better performance in modern browsers, use $("your-pure-css-selector").slice(index) instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜