,因此CSS应该是:
.rest:nth-child(6), .rest:nth-child(7), .rest:nth-child(8), .rest:nth-child(9), .rest:nth-child(10){
display:none;
}
或者
.rest:nth-of-type(6), .rest:nth-of-type(7), .rest:nth-of-type(8), .rest:nth-of-type(9), .rest:nth-of-type(10){
display:none;
}
现在你一定想知道nth-child
和nth-of-type
之间有什么区别,那么这就是它们的区别:
假设html如下:
<div class="one">...</div>
<div class="two">...</div>
<div class="three">...</div>
<div class="four">...</div>
<div class="five">...</div>
<p class="rest">...</p>
<p class="rest">...</p>
<p class="rest">...</p>
<p class="rest">...</p>
<p class="rest">...</p>
在上面的HTML中,
.rest
元素的类型与其他元素不同,其他元素是div,而这些元素是段落。因此,在这种情况下,如果您使用
nth-child
,您需要写成这样:
.rest:nth-child(6), .rest:nth-child(7), .rest:nth-child(8), .rest:nth-child(9), .rest:nth-child(10){
display:none;
}
但是如果你使用nth-of-type选择器,CSS代码可以变成这样。
.rest:nth-of-type(1), .rest:nth-of-type(2), .rest:nth-of-type(3), .rest:nth-of-type(4), .rest:nth-of-type(5){
display:none;
}
由于
.rest
元素的类型是
<p>
,因此这里的
nth-of-type
检测到了
.rest
的类型,然后在
<p>
的第1、2、3、4、5个元素上应用了CSS。
:nth-child(An+B of S)
。 - mfluehr