css选择器中:first-child与:first-of-type的区别
来源:互联网 发布:windows用户密码是什么 编辑:程序博客网 时间:2024/04/29 13:26
:first-child选择器是css2中定义的选择器,从字面意思上来看也很好理解,就是第一个子元素。比如有段代码:
p:first-child 匹配到的是p元素,因为p元素是div的第一个子元素;
h1:first-child 匹配不到任何元素,因为在这里h1是div的第二个子元素,而不是第一个;
span:first-child 匹配不到任何元素,因为在这里两个span元素都不是div的第一个子元素;
:first-child 匹配到的是p元素,因为在这里div的第一个子元素就是p。
然后,在css3中又定义了:first-of-type这个选择器,这个跟:first-child有什么区别呢?还是看那段代码:
p:first-of-type 匹配到的是p元素,因为p是div的所有为p的子元素中的第一个,事实上这里也只有一个为p的子元素;
h1:first-of-type 匹配到的是h1元素,因为h1是div的所有为h1的子元素中的第一个,事实上这里也只有一个为h1的子元素;
span:first-of-type 匹配到的是第三个子元素span。这里div有两个为span的子元素,匹配到的是第一个。
:first-of-type 匹配到的是p元素
所以,通过以上两个例子可以得出结论:
:first-child 匹配的是某父元素的第一个子元素,可以说是结构上的第一个子元素。
:first-of-type 匹配的是该类型的第一个,类型是指什么呢,就是冒号前面匹配到的东西,比如 p:first-of-type,就是指所有p元素中的第一个。这里不再限制是第一个子元素了,只要是该类型元素的第一个就行了,当然这些元素的范围都是属于同一级的,也就是同辈的。
同样类型的选择器 :last-child 和 :last-of-type、:nth-child(n) 和 :nth-of-type(n) 也可以这样去理解。
- css选择器中:first-child与:first-of-type的区别
- css选择器中:first-child与:first-of-type的区别
- css选择器中:first-child与:first-of-type的区别
- css选择器中:first-child与:first-of-type的区别
- css选择器中:first-child与:first-of-type的区别///CSS3伪类nth-of-type(n)用法详解
- CSS选择器中first-child和first-of-type的区别
- 在css选择器中:first-child与:first-of-type的用法
- first-of-type与first-child区别
- jQuery 选择器 first-child和first-of-type、 last-child和last-of-type的区别
- jquery 中 first 与 first-child选择器的区别
- CSS选择器 first-child 、first-of-type 和 nth-of-type()
- first-child与first-of-type
- css选择器:first-of-type
- css伪类选择器之:first-child和:first-of-type
- CSS :first-child 选择器
- jQuery中 :first选择器,first()和:first-child选择器的区别
- first-child first-of-type last-child last-of-type 伪类选择器总结
- first-of-type选择器
- 易飞免费辅助:集成了WEB端、安卓端、微信端和PC端 一:WEB端内网
- cocos2d-js 3.0 热更新
- 时钟的Java编程
- 交作业 php100 第六讲 遍历所有数组
- java+内存分配及变量存储位置的区别
- css选择器中:first-child与:first-of-type的区别
- DialogFragment中ListView的子项显示不可用
- 在CentOS系统中临时禁止一个用户
- 开始记录编程的点点滴滴
- C语言中三种常见排序算法分析
- codeforces 283C
- 易飞免费辅助:集成了WEB端、安卓端、微信端和PC端 二:微信端
- MySQL多实例配置(一)
- TCP/IP基础(一)