开发者

jquery: Finding the last child from a selector

开发者 https://www.devze.com 2023-02-18 02:00 出处:网络
For example, I\'ve got a selector here var $myNeeds = $(\'.history\'); This selector would have multiple divs inside, and now I want to get th开发者_C百科e last child from it, how to do this?

For example, I've got a selector here

var $myNeeds = $('.history');

This selector would have multiple divs inside, and now I want to get th开发者_C百科e last child from it, how to do this?

I tried $myNeeds.last(), this won't work!


Alternatively....

$myNeeds.find('>:last-child')

jsFiddle.


You're almost there...

$myNeeds.children().last()

The last method gets the last element in your set.
You want the last child of your set, so you need to get a new set with the children.

You could also write

$('.history > :last-child')

or

$myNeeds.children(":last")
0

精彩评论

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