Determining if the element is the last child of its parent
Using jQuery, is there a quick way of knowing if an element is its parent's last child?
Example:
<ul>
<li id="a"></li>
<li id="b"></li>
<li id="c"></li>
</ul>
$('#b').isLastChild(); //should r开发者_如何学运维eturn FALSE
$('#c').isLastChild(); //should return TRUE
Use :last-child selector together with .is()
$('#c').is(':last-child')
You can use .is()
with :last-child
like this:
$('#b').is(":last-child"); //false
$('#c').is(":last-child"); //true
You can test it here. Another alternative is to check .next().length
, if it's 0
it's the last element.
if($('#b').is(":last-child")){
}
精彩评论