css3中nth-child选择器和nth-of-type选择器的区别

来源:互联网 发布:hl5590dn网络共享 编辑:程序博客网 时间:2024/05/01 15:32

:nth-child可以选择父元素下的子元素,:nth-of-type也可以。但是它们也有区别? 


其实区别很简单::nth-of-type为什么要叫:nth-of-type?因为它是以"type"来区分的。也就是说:e:nth-of-type(n)是指父元素下第n个e元素, 


而e:nth-child(n)是指父元素下第n个元素且这个元素为e,若不是,则选择失败。


代码如下

<style>        /*选中的是<span>11</span>*/        /*div span:nth-child(3){ color:red;}*/        /*选中的是<span>33</span>*/        div span:nth-of-type(3){ color:red;}    </style></head><body>    <div>        <p>55</p>        <p>55</p>        <span>11</span>        <span>22</span>        <span>33</span>        <span>44</span>    </div></body>

0 0
原创粉丝点击