CSS3 nth-of-type(n)选择器
“:nth-of-type(n)
”选择器和“:nth-child(n)
”选择器非常类似,不同的是它只计算父元素中指定的某种类型的子元素。当某个元素中的子元素不单单是同一种类型的子元素时,使用“:nth-of-type(n)”选择器来定位于父元素中某种类型的子元素是非常方便和有用的。在“:nth-of-type(n)”选择器中的“n”和“:nth-child(n)”选择器中的“n”参数也一样,可以是具体的整数,也可以是表达式,还可以是关键词。
例如:第1行、第7行及第13行,输入正确的代码,将容器“div.wrapper”中的偶数段落和奇数Div背景设置为橙色。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>属性选择器</title> <style> .wrapper > div:nth-of-type(2n+1), .wrapper > p:nth-of-type(2n){ background: orange; } /*或者*/ .wrapper > div:nth-of-type(2n+1), .wrapper > p:nth-of-type(2n){ background: orange; } /*或者*/ .wrapper > div:nth-of-type(odd), .wrapper > p:nth-of-type(even){ background: orange; } </style> </head> <body> <div class="wrapper"> <div>我是一个Div元素</div> <p>我是一个段落元素</p> <div>我是一个Div元素</div> <p>我是一个段落</p> <div>我是一个Div元素</div> <p>我是一个段落</p> <div>我是一个Div元素</div> <p>我是一个段落</p> <div>我是一个Div元素</div> <p>我是一个段落</p> <div>我是一个Div元素</div> <p>我是一个段落</p> <div>我是一个Div元素</div> <p>我是一个段落</p> <div>我是一个Div元素</div> <p>我是一个段落</p> </div> </body> </html>
Dcr163的博客
http://dcr163.cn/127.html(转载时请注明本文出处及文章链接)