如何选择css nth-child列表中的最后2个项目? [英] How do I choose the last 2 items in a list with css nth-child?

查看:1369
本文介绍了如何选择css nth-child列表中的最后2个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能吗?如果没有,有没有办法使用jQuery?

Is it possible? If not, is there a way to do it with jQuery?

推荐答案

很遗憾,这是不可能的..忽略此答案,并查看下面的拆分器答案.

如果可能的话,它看起来就像...

If it were to be possible, it would look something like...

ul li:last-child+li {...}

但这是行不通的,因为+会选择最后一个孩子之后的直接姐妹(当然没什么).没有直接的上一个选择器.

But this doesn't work, because + will select the immediate sibling after last-child (which is nothing, of course). There is no immediate previous selector.

使用jQuery实现此目的的方法有多种,最有效的方法是...

There are different ways of achieving this with jQuery, the most performant would be...

var lastItems = $("#list li");
lastItems.slice(lastItems.length - 2).addClass("whatever");

http://jsfiddle.net/qkzdJ/

这篇关于如何选择css nth-child列表中的最后2个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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