css选择器中:first-child与:first-of-type的区别
来源:互联网 发布:macbook怎么显示软件 编辑:程序博客网 时间:2024/05/16 07:21
介绍得简单明了,怒转~ http://www.cnblogs.com/2050/p/3569509.html
————————————————————————————————————————————————————————————————————————————
:first-child选择器是css2中定义的选择器,从字面意思上来看也很好理解,就是第一个子元素。比如有段代码:
div p:first-child 匹配到的是p元素,因为p元素是div的第一个子元素;
div h1:first-child 匹配不到任何元素,因为在这里h1是div的第二个子元素,而不是第一个;
div span:first-child 匹配不到任何元素,因为在这里两个span元素都不是div的第一个子元素;
然后,在css3中又定义了:first-of-type这个选择器,这个跟:first-child有什么区别呢?还是看那段代码:
div p:first-of-type 匹配到的是p元素,因为p是div的所有类型为p的子元素中的第一个;
div h1:first-of-type 匹配到的是h1元素,因为h1是div的所有类型为h1的子元素中的第一个;
div span:first-of-type 匹配到的是第三个子元素span。这里div有两个为span的子元素,匹配到的是它们中的第一个。
所以,通过以上两个例子可以得出结论:
:first-child 匹配的是某父元素的第一个子元素,可以说是结构上的第一个子元素。比如上面的div p:first-child 是匹配div中的第一个元素且此元素是p;
:first-of-type 匹配的是某父元素下相同类型子元素中的第一个,比如div p:first-of-type,就是指div中所有类型为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选择器
- 虚拟机安装centos7
- Codeforces Round #434 (Div. 2): D. Polycarp's phone book(字典树)
- 异步任务HttpUrlconnect 的GET 网络请求
- 二叉搜索树
- 后台接收前台 String json 取单个值问题-java
- css选择器中:first-child与:first-of-type的区别
- 技术方案模板
- 数据库操作的作业练习
- PostgreSQL、Greenplum 日常监控 和 维护任务
- 自定义控件(2)
- C#托管代码与C++非托管代码互相调用1
- C到C++的过渡
- Openlayers3中SVG图加载
- oracle函数(二)