jQuery选择器前-后 [英] jquery selector before-last

查看:114
本文介绍了jQuery选择器前-后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态列表,需要选择最后一项之前的内容.

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?

推荐答案

您可以使用 .eq() 为负值(-1为最后)从末尾获取n,如下所示:

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"

您可以在此处进行测试.

这篇关于jQuery选择器前-后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆