【发布时间】:2015-10-30 19:40:12
【问题描述】:
是否可以使用parent child:nth-child() 并且只适用于儿童?
html 如下:
<body>
<div>div 1</div>
<div>
div 2
<div>div 2.1</div>
<div>div 2.2</div>
</div>
</body>
我想用 CSS 选择:div 1 & div 2,我用过:
body div:nth-of-type(1) {
background: red;
color: white;
}
body div:nth-child(2) {
background: blue;
}
但是div 2.1 & 2.21 也被选中了。 JSFiddle
是否可以进行非递归选择?如果不是,我猜唯一的解决方案是 clases 或 id's ..
【问题讨论】: