[英]nth-of-type not behaving as expected
HTML HTML
<div id="content">
<div id="ok" class="content-block warning"><div></div><span>✓ Everything is OK!</span> </div>
<div id="module_1" class="content-block small">asfdasdf</div>
<div id="module_2" class="content-block small">asdfasdf</div>
<div id="module_3" class="content-block small">asfasdf</div>
<div id="module_4" class="content-block small">asdfasdf</div>
<div id="module_5" class="content-block small">asfasdf</div>
<div id="module_6" class="content-block small">asdfasdf</div>
<div id="module_7" class="content-block big"></div>
<div id="module_8" class="content-block big"></div>
</div>
CSS CSS
.small:nth-of-type(1) {
background-color: black;
border-style: solid;
border-width: 15px;
border-color: black;
}
.small:nth-of-type(2) {
background-color: red;
}
In this example: http://jsfiddle.net/CNBsY/2/ I can't quite figure out why nth-of-type(1)
doesn't select anything while nth-of-type(2)
selects the first thing I want to select. 在此示例中: http : //jsfiddle.net/CNBsY/2/我不太清楚为什么
nth-of-type(1)
没有选择任何东西,而nth-of-type(2)
选择了第一件事我要选择。 Does anybody understand why this is happening? 有人知道为什么会这样吗?
That's because it's nth-of-type
, not nth-of-class
. 那是因为它是
nth-of-type
的nth-of-type
,而不是nth-of-class
。
First div
in your HTML does not have small
class, so there is no element matching both .small
and nth-of-type(1)
. 第一
div
在你的HTML没有small
课,所以没有元件匹配两种.small
和nth-of-type(1)
The pseudo selector :nth-of-type()
selects the type of tag . 伪选择器
:nth-of-type()
选择标签的类型 。
In your example it is a div
. 在您的示例中,它是一个
div
。 As you set the selector on the class ( .small:nth-of-type(1)
) the css is aplied only if the first div has that class which isn't the case. 当您在类(
.small:nth-of-type(1)
)上设置选择器时,仅在第一个div具有该类的情况下才.small:nth-of-type(1)
css。
Try this : 尝试这个 :
HTML HTML
<div id="content">
<span id="ok" class="content-block warning">✓ Everything is OK!</span>
<ul>
<li id="module_1" class="content-block small">asfdasdf</li>
<li id="module_2" class="content-block small">asdfasdf</li>
<li id="module_3" class="content-block small">asfasdf</li>
<li id="module_4" class="content-block small">asdfasdf</li>
<li id="module_5" class="content-block small">asfasdf</li>
<li id="module_6" class="content-block small">asdfasdf</li>
<li id="module_7" class="content-block big"></li>
<li id="module_8" class="content-block big"></li>
</ul>
</div>
CSS CSS
li:nth-of-type(1) {
background-color: black;
border-style: solid;
border-width: 15px;
border-color: black;
}
li:nth-of-type(2) {
background-color: red;
}
It's a better semantic. 这是更好的语义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.