定位前2个孩子或后2个孩子 [英] Targeting first 2 children or last 2 children

查看:117
本文介绍了定位前2个孩子或后2个孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道很多伪类(第一个孩子,最后一个孩子,第n个孩子),但是我在选择列表中的前2个孩子或最后2个孩子时遇到了麻烦,该列表是动态的,并且会更改所有时间,所以我无法根据计数李的目标来定位

I know a bunch of the pseudo classes (first-child, last-child, nth-child) but I am having trouble selecting the first 2 children in a list or the last 2, the list is dynamic and changes all the time so i cant target based on counting the li's

<ul>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
</ul>

推荐答案

对于前两个孩子,您可以使用:

For the first two children you can use:

ul li:nth-child(-n + 2) {
    color: orange;
}

http://jsfiddle.net/nYnSz/1/

对于最后两个:

ul li:nth-last-child(-n + 2) {
    color: orange;
}

http://jsfiddle.net/nYnSz/

这篇关于定位前2个孩子或后2个孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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