开发者

jquery selector before-last

开发者 https://www.devze.com 2023-01-19 06:51 出处:网络
I have a dynamic list and need to select the before last item. <ul class=\"album\"> <li id=\'li-1\'></li>

I have a dynamic list and need to select the before last item.

<ul class="album">
    <li id='li-1'></li>
    <!-- ... -->
    <li id='li-8'></li>
    <li id='li-9'></li>
    <li class='drop开发者_如何学运维-placeholder'>drag your favorites here</li>
</ul>

var lastLiId = $(".album li:last").attr("id"); // minus one?


You can use .eq() with a negative value (-1 is last) to get n from the end, like this:

$(".album li").eq(-2).attr("id"); // gets "li-9"

You can test it here.


Probably a neater way but how about:

var lastLiId = $(".album li:last").prev("li").attr("id");
0

精彩评论

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

关注公众号