how eq(0) works with DOM
I want to know in below case will the browser sto开发者_Go百科p traversing the DOM after getting the first .myclass
$(".myclass").eq(0)
I don't believe so.
$(".myclass")
Will return a jQuery object that (behind the scenes) contains an array of all matching DOM elements.
You are then calling a method on that object to return the first element.
If you want to avoid this, you need your selector to only select one element. Take a look at the documentation for selectors:
http://docs.jquery.com/Selectors
Try this instead:
$(".myclass:first").eq(0)
No. In your example, the browser will fetch all the elements with class myclass
. Then you apply a filter for the first element.
You might be able to call .end()
after your .eq(0)
to get the whole elements back.
You need the selector eq
, like in the following example $(".myclass:eq(0)")
.
精彩评论