小白教程
所有教程
关于
Search
172.69.7.193
172.69.7.193
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Sel nth of type”的源代码
本页内容
上一节:
Sel_nth_last_child
下一节:
Sel_nth_last_of_type
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE::nth-of-type(n)}}[[Category:css selectors|34]] = CSS3 :nth-of-type() Selector = [[css selectors|完整CSS选择器参考手册]] 指定每个p元素匹配同类型中的第2个同级兄弟元素的背景色: <sample title="" desc="" lang="html" hererun="1"> p:nth-of-type(2) { background:#ff0000; } </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <style> p:nth-of-type(2) { background:#ff0000; } </style> </head> <body> <h1>This is a heading</h1> <p>The first paragraph.</p> <p>The second paragraph.</p> <p>The third paragraph.</p> <p>The fourth paragraph.</p> </body> </html> </run> == 定义和用法 == :nth-of-type(n)选择器匹配同类型中的第n个同级兄弟元素。 n可以是一个数字,一个关键字,或者一个公式。 '''提示:''' 请参阅 [[sel nth child|:nth-child()]] 选择器。该选择器匹配父元素中的第n个子元素。Look at the [[sel_nth child.asp|:nth-child()]] 。 == 浏览器支持 == 表格中的数字表示支持该属性的第一个浏览器版本号。 {| class="table table-striped table-hover" ! 选择器 ! Chrome ! Internet Explorer / Edge ! Firefox ! Safari ! Opera |- | :nth-of-type() | 4.0 | 9.0 | 3.5 | 3.2 | 9.6 |} == 更多实例 == == 实例 1 == 奇数和偶数是可以作为关键字使用用于相匹配的子元素,其索引是奇数或偶数(该索引的第一个子节点是1)。 在这里,我们为奇数和偶数p元素指定两个不同的背景颜色: <sample title="" desc="" lang="html" hererun="1"> p:nth-of-type(odd) { background:#ff0000; } p:nth-of-type(even) { background:#0000ff; } </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <style> p:nth-of-type(odd) { background:#ff0000; } p:nth-of-type(even) { background:#0000ff; } </style> </head> <body> <h1>This is a heading</h1> <p>The first paragraph.</p> <p>The second paragraph.</p> <p>The third paragraph.</p> </body> </html> </run> == Example 2 == 使用公式(an+ b).描述:a代表一个循环的大小,N是一个计数器(从0开始),以及b是偏移量。 在这里,我们对所有索引是3的倍数的p元素指定了背景颜色: <sample title="" desc="" lang="html" hererun="1"> p:nth-of-type(3n+0) { background:#ff0000; } </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <style> p:nth-of-type(3n+0) { background:#ff0000; } </style> </head> <body> <h1>This is a heading</h1> <p>The first paragraph.</p> <p>The second paragraph.</p> <p>The third paragraph.</p> <p>The fourth paragraph.</p> <p>The fifth paragraph.</p> <p>The sixth paragraph.</p> <p>The seventh paragraph.</p> <p>The eight paragraph.</p> <p>The ninth paragraph.</p> </body> </html> </run> [[css selectors|完整CSS选择器参考手册]]
返回至“
Sel nth of type
”。
上一节:
Sel_nth_last_child
下一节:
Sel_nth_last_of_type