是否可以使用 CSS3 选择器:first-of-type
选择具有给定类名的第一个元素?我的测试没有成功,所以我想是不是?
代码 (http://jsfiddle.net/YWY4L/):
p:first-of-type {color:blue}
p.myclass1:first-of-type {color:red}
.myclass2:first-of-type {color:green}
<div>
<div>This text should appear as normal</div>
<p>This text should be blue.</p>
<p class="myclass1">This text should appear red.</p>
<p class="myclass2">This text should appear green.</p>
</div>
不,不可能只使用一个选择器。这:first-of-type
伪类选择其第一个元素type (div
, p
, ETC)。将类选择器(或类型选择器)与该伪类一起使用意味着选择具有给定类(或给定类型)的元素and是同类产品中的第一个。
不幸的是,CSS 没有提供:first-of-class
仅选择类的第一次出现的选择器。作为解决方法,您可以使用如下内容:
.myclass1 { color: red; }
.myclass1 ~ .myclass1 { color: /* default, or inherited from parent div */; }
给出了解决方法的解释和插图here and here.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)