开发者

jQuery best practice for $($(".elem")[0])?

开发者 https://www.devze.com 2023-03-13 15:28 出处:网络
Is there a way to only use ONE dollar sign instead of doing this? $($(\".开发者_StackOverflowelem\")[0]).hide()

Is there a way to only use ONE dollar sign instead of doing this?

$($(".开发者_StackOverflowelem")[0]).hide()

I could use :first, but what about if I wanted the third or so:

$($(".elem")[2]).hide()


Use .eq() function

$(".elem").eq(3).hide()

Description: Reduce the set of matched elements to the one at the specified index.

.eq( index )

indexAn integer indicating the 0-based position of the element.

And

.eq( -index )

-indexAn integer indicating the position of the element, counting backwards from the last element in the set.

And it is 0 index based, so third would be .eq(2)


You can use, :eq

$('.test:eq(2)').hide(); //hides the third encounter of an element with class .test

There is also :nth-child( x ) but it grabs a child element.

Read more,

http://api.jquery.com/eq-selector/

http://api.jquery.com/nth-child-selector/

0

精彩评论

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