开发者

Determining if the element is the last child of its parent

开发者 https://www.devze.com 2023-01-26 03:41 出处:网络
Using jQuery, is there a quick way of knowing if an element is its parent\'s last child? Example: <ul>

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

精彩评论

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