引言
在Chrome DevTools中,当你输入$('.btn')
时,背后是CSS选择器的强大能力。作为前端开发的基石,选择器不仅决定了样式的应用范围,更直接影响页面性能和代码可维护性,同时也是爬虫自动化的基础。本文将带你从基础到高级,全面掌握CSS选择器的精髓!
一、基础选择器(5大核心)
1.1 元素选择器
css
/* 语法 */
元素名 { 样式声明 }
/* 示例 */
h1 {
color: #333;
font-size: 2rem;
}
1.2 类选择器
css
/* 语法 */
.类名 { 样式声明 }
/* 示例 */
.btn {
padding: 8px 16px;
border-radius: 4px;
}
1.3 ID选择器
css
/* 语法 */
#id名 { 样式声明 }
/* 示例 */
#header {
background: linear-gradient(to right, #ff8a00, #da1b60);
}
1.4 通配符选择器
css
/* 语法 */
* { 样式声明 }
/* 示例 */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
1.5 属性选择器(基础)
css
/* 语法 */
[属性名] { 样式声明 }
/* 示例 */
[disabled] {
opacity: 0.6;
cursor: not-allowed;
}
二、组合选择器(6种关系)
2.1 后代选择器
css
/* 语法 */
祖先 后代 { 样式声明 }
/* 示例 */
nav ul li {
display: inline-block;
}
2.2 子元素选择器
css
/* 语法 */
父元素 > 子元素 { 样式声明 }
/* 示例 */
.menu > li {
border-bottom: 1px solid #eee;
}
2.3 相邻兄弟选择器
css
/* 语法 */
元素 + 相邻兄弟 { 样式声明 }
/* 示例 */
h2 + p {
margin-top: 0;
color: #666;
}
2.4 相邻兄弟选择器
css
/* 语法 */
元素 + 兄弟 { 样式声明 }
/* 示例 */
h1 + p {margin-top:50px;}
2.5 多选择器组合
css
/* 语法 */
选择器1, 选择器2 { 样式声明 }
/* 示例 */
h1, h2, h3 {
font-family: 'Helvetica Neue', sans-serif;
}
三、伪类选择器
3.1 语法
css
/* 伪类的语法:*/
selector:pseudo-class {property:value;}
/* CSS类也可以使用伪类:*/
selector.class:pseudo-class {property:value;}
3.2 示例链接状态
css
/* 未访问链接 */
a:link { color: blue; }
/* 已访问链接 */
a:visited { color: purple; }
/* 悬停状态 */
a:hover { text-decoration: underline; }
/* 激活状态 */
a:active { color: red; }
3.3 所有CSS伪类/元素
选择器 | 示例 | 示例说明 |
---|---|---|
:checked | input:checked | 选择所有选中的表单元素 |
:disabled | input:disabled | 选择所有禁用的表单元素 |
:empty | p:empty | 选择所有没有子元素的p元素 |
:enabled | input:enabled | 选择所有启用的表单元素 |
:first-of-type | p:first-of-type | 选择的每个 p 元素是其父元素的第一个 p 元素 |
:in-range | input:in-range | 选择元素指定范围内的值 |
:invalid | input:invalid | 选择所有无效的元素 |
:last-child | p:last-child | 选择所有p元素的最后一个子元素 |
:last-of-type | p:last-of-type | 选择每个p元素是其母元素的最后一个p元素 |
:not(selector) | :not(p) | 选择所有p以外的元素 |
:nth-child(n) | p:nth-child(2) | 选择所有 p 元素的父元素的第二个子元素 |
:nth-last-child(n) | p:nth-last-child(2) | 选择所有p元素倒数的第二个子元素 |
:nth-last-of-type(n) | p:nth-last-of-type(2) | 选择所有p元素倒数的第二个为p的子元素 |
:nth-of-type(n) | p:nth-of-type(2) | 选择所有p元素第二个为p的子元素 |
:only-of-type | p:only-of-type | 选择所有仅有一个子元素为p的元素 |
:only-child | p:only-child | 选择所有仅有一个子元素的p元素 |
:optional | input:optional | 选择没有"required"的元素属性 |
:out-of-range | input:out-of-range | 选择指定范围以外的值的元素属性 |
:read-only | input:read-only | 选择只读属性的元素属性 |
:read-write | input:read-write | 选择没有只读属性的元素属性 |
:required | input:required | 选择有"required"属性指定的元素属性 |
:root | root | 选择文档的根元素 |
:target | #news:target | 选择当前活动#news元素(点击URL包含锚的名字) |
:valid | input:valid | 选择所有有效值的属性 |
:link | a:link | 选择所有未访问链接 |
:visited | a:visited | 选择所有访问过的链接 |
:active | a:active | 选择正在活动链接 |
:hover | a:hover | 把鼠标放在链接上的状态 |
:focus | input:focus | 选择元素输入后具有焦点 |
:first-letter | p:first-letter | 选择每个<p> 元素的第一个字母 |
:first-line | p:first-line | 选择每个<p> 元素的第一行 |
:first-child | p:first-child | 选择器匹配属于任意元素的第一个子元素的 <p> 元素 |
:before | p:before | 在每个<p> 元素之前插入内容 |
:after | p:after | 在每个<p> 元素之后插入内容 |
:lang(language) | p:lang(it) | 为<p> 元素的lang属性选择一个开始值 |
结语
如果你喜欢本教程,记得点赞+收藏!关注我获取更多JavaScript开发干货。