简体   繁体   English

nth-of-type不在div上工作

[英]nth-of-type not working on div

I'm confused with nth-of-type selector. 我和nth-of-type选择器混淆了。

I have tried this snippet 我试过这个片段

 .red:nth-of-type(1){ color:red; } 
 <div class="home"> <span>blah</span> <p class="red">first</p> <p class="red">second</p> <p class="red">third</p> <div class="red">fourth</div> </div> 

It gets both p and div with class red and changes its color 它得到p和div的类红色并改变它的颜色

Now i'm stuck with this example 现在我坚持这个例子

 section .foo:nth-of-type(1){ background:red; } .parent .foo:nth-of-type(1){ background:red; } 
 <section> <p class="foo">Little</p> <p>Piggy</p> <div>...</div> <div class="foo">border...</div> </section> <div class="parent"> <i class="foo">1</i> <i>x</i> <i class="foo">2</i> <b class="foo">3</b><b>x</b><b class="foo">4</b> </div> 

I was expecting both p and div in the section with class foo to get red background but it does not work, it works well when div is replaced with span 我期望在foo类的部分中使用p和div来获得红色背景但是它不起作用,当div被span替换时它很好用

but,other styles to parent div in the code works correctly and styles i and b 但是,代码中父div的其他样式正常工作,样式i和b

I know this is a duplicate of CSS selector for first element with class 我知道这是带有类的第一个元素CSS选择器的副本

It is because the div with class 'foo' is not the first child of that type, it is the second. 这是因为具有类'foo'的div不是该类型的第一个子类,它是第二个。 The selector will only match elements that are the first of there type and have the class 'foo'. 选择器只匹配那些类型的第一个元素并且具有类'foo'。

It doesn't match the first of that type AND class like you are expecting 它与您期望的类型AND类中的第一个不匹配

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM