CSS 3选择器:具有类名的第一类型?

CSS 3选择器:具有类名的第一类型?

是否可以使用CSS 3选择器?: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>


呼如林
浏览 616回答 3
3回答

慕桂英4014372

不,只用一个选择器是不可能的。这个:first-of-type类选择其第一个元素。类型&nbsp;(div,&nbsp;p(等)使用类选择器(或类型选择器)和该伪类意味着,如果元素具有给定的类(或者是给定的类型),则可以选择它。和是它的兄弟姐妹中的第一种。不幸的是,CSS没有提供:first-of-class仅选择类的第一次出现的选择器。作为一种解决办法,您可以使用以下内容:.myclass1&nbsp;{&nbsp;color:&nbsp;red;&nbsp;}.myclass1&nbsp;~&nbsp;.myclass1&nbsp;{&nbsp;color:&nbsp;/*&nbsp;default,&nbsp;or&nbsp;inherited&nbsp;from&nbsp;parent&nbsp;div&nbsp;*/;&nbsp;}本文给出了解决方案的说明和说明。这里和这里.
打开App,查看更多内容
随时随地看视频慕课网APP