开发者

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")){

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜