是否可以使用CSS3选择器:first-of-type
来选择具有给定类名称的第一个元素?我的测试没有成功,所以我认为这不是?CSS3选择器:类名称的第一个类型?
守则(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>
该作品如果你只有2个元素,但失败更多): –
否@justNik这对多个元素有效。 '.myclass1'选择器将选择'.myclass1'的每个元素。选择器'.myclass1〜.myclass1'使用通用兄弟组合器来选择每个具有'.myclass1'类的元素,该类是具有'.myclass1'类的元素的后续兄弟。这是详细解释[** here **](http://stackoverflow.com/questions/2717480/css-selector-for-first-element-with-class/8539107#8539107)。 – WebWanderer