对于":nth-child"前面加空格造成区别个人理解
来源:互联网 发布:调整型内衣品牌 知乎 编辑:程序博客网 时间:2024/05/07 02:04
在学习jQuery到子元素过滤选择器的":nth-child"选择器时,我发现在使用":nth-child"代码时,前面是否加空格对于页面显示的效果会有不同,具体如下:
- 不加空格时:
$('div.one:nth-child(1)').css('background','darkgoldenrod');
显示效果:
- 加空格时:
$('div.one :nth-child(1)').css('background','darkgoldenrod');
显示效果:
区别很明显,所以个人理解:
- :nth-child前方加空格是因为jQuery的层次选择器中的$('ancestor descendant')的逻辑,也就是说加了空格以后,:nth-child所获取的是前面ancestor 元素的后代元素descendant。以上面例子来解释:
- 当:nth-child前面不加空格时,:nth-child是以div.one为索引获取条件的,所以其获取的是div.one元素中索引号为1的元素。
- 当:nth-child前面加上空格时,:nth-child是以div.one的后代元素为索引条件的,也就是说在加了空格的情况下,该选择器所选择的才是class为one的<div>父辈元素下的索引号为1的元素。
PS:在提问区的两位大神也帮忙解答了我的疑问,答案和我个人的理解一样,使得自己的理解得到了确认,再次感谢下面两位老师:
0 0
- 对于":nth-child"前面加空格造成区别个人理解
- jquery选择器:nth-child()与空格:eq() 的区别;
- jQuery中 :selected前面加空格的理解
- CSS3中:nth-child和:nth-of-type的区别深入理解
- CSS3中:nth-child和:nth-of-type的区别深入理解
- nth-child和nth-of-type区别
- nth-child()与nth-of-type()区别
- :nth-child(index)和nth-child(index)选择器的区别
- :nth-child 和 :nth-type-of 的区别
- :nth-child 和 :nth-type-of 的区别
- CSS3中:nth-child和:nth-of-type的区别
- CSS3中:nth-child和:nth-of-type的区别
- :nth-child(n)与:nth-of-type(n)的区别
- :nth-child和:nth-of-type的区别
- 关于:nth-child和:nth-of-type的区别
- nth-type-of nth-child(n) eq(n)区别
- :nth-child和:nth-of-type的区别
- nth-child和nth-of-type的区别
- git提交更改
- 到底有没有神???
- Timber(对Log类封装的一个工具)
- css3高级滤镜-渐隐效果
- SQL语句(union、insert和约束)
- 对于":nth-child"前面加空格造成区别个人理解
- 如何解决ajax跨域问题(转)
- css3高级滤镜-切换滤镜
- java中abstract怎么使用
- 《More Effective C++》学习笔记之一引用与指针
- ZOJ 3860: - Find the Spy
- 对数据库数据操作之查找
- POJ-1201-Intervals(差分约束)
- 未加载mfc120ud.i386.pdb:VS2013调试MFC程序解决方法