是否可以使用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