批改状态:合格
老师批语:选择器核心的就那么点知识, 其它的都是扩展, 很容易掌握
| 序号 | 选择器 | 描述 | 举例 |
|---|---|---|---|
| 1 | 元素选择器 | 根据元素标签名称进行匹配 | div {...} |
| 2 | 群组选择器 | 同时选择多个不同类型的元素 | h1,h2,h3{...} |
| 3 | 通配选择器 | 选择全部元素,不区分类型 | * {...} |
| 4 | 属性选择器 | 根据元素属性进行匹配 | *[...] |
| 5 | 类选择器 | 根据元素 class 属性进行匹配 | *.active {...} |
| 6 | id 选择器 | 根据元素 id 属性进行匹配 | *#top {...} |
*“可以省略
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>简单选择器</title><style>/* 使用九宫格来演示简单选择器 *//* 类选择器:选择一类元素,也属于属性选择器一种,对应着元素中的class */.container {width: 300px;height: 300px;display: grid;grid-template-columns: repeat(3, 1fr);gap: 5px;}/* 类选择器 */.item {font-size: 2rem;background-color: lightskyblue;display: flex;justify-content: center;align-items: center;}/* 元素选择器 */body {background-color: lightcyan;}/* 多个类选择器 */.item.center {background-color: lightgreen;}/* id选择器 */#first {background-color: lime;}/* id, class可以添加到任何元素上, 前面的元素限定符默认就是*,所以可省略 */div#first {background-color: lime;}/* 类选择器, class 权重 大于 标签 */.item#first {background-color: lightpink;}/* id的权重大于class */*#first.item {background-color: violet;}/* 标签 < class属性 < id属性 *//* 属性选择器 */*.item[title="php"] {background-color: lightcoral;}*#first,.item.center,.item[title="hello"] {background-color: black;color: white;}</style></head><body><div class="container"><div class="item" id="first">1</div><div class="item">2</div><div class="item" title="php">3</div><div class="item">4</div><div class="item center">5</div><div class="item" title="hello">6</div><div class="item">7</div><div class="item">8</div><div class="item">9</div></div></body></html>
| 序号 | 角色 | 描述 |
|---|---|---|
| 1 | 祖先元素 | 拥有子元素,孙元素等所有层级的后代元素 |
| 2 | 父级元素 | 仅拥有子元素层级的元素 |
| 3 | 后代元素 | 与其它层级元素一起拥有共同祖先元素 |
| 4 | 子元素 | 与其它同级元素一起拥有共同父级元素 |
| 序号 | 选择器 | 操作符 | 描述 | 举例 |
|---|---|---|---|---|
| 1 | 后代选择器 | 空格 |
选择当前元素的所有后代元素 | div p, body * |
| 2 | 父子选择器 | > |
选择当前元素的所有子元素 | div > h2 |
| 3 | 同级相邻选择器 | + |
选择拥有共同父级且相邻的元素 | li.red + li |
| 4 | 同级所有选择器 | ~ |
选择拥有共同父级的后续所有元素 | li.red ~ li |

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>上下文选择器</title><style>.container {width: 300px;height: 300px;display: grid;grid-template-columns: repeat(3, 1fr);gap: 5px;}/* 类选择器 */.item {font-size: 2rem;background-color: lightskyblue;display: flex;justify-content: center;align-items: center;}/* 后代选择器 */.container div {border: 1px solid coral;}/* 父子选择器,只有外层的div受影响 */body > div {border: 3px solid green;}/* 使用后代选择器模拟父子选择器 *//* body div.container {border: 3px solid green;} *//* 同级相邻选择器 *//* 选择与第5个相邻的,即后面的"一个"元素 *//* .item.center + .item {background-color: lightgreen;} *//* 同级所有选择器 *//* 选择与第5个后面的,有共同父级的所有兄弟元素 */.item.center ~ .item {background-color: lightgreen;}</style></head><body><div class="container"><div class="item">1</div><div class="item">2</div><div class="item">3</div><div class="item">4</div><div class="item center">5</div><div class="item">6</div><div class="item">7</div><div class="item">8</div><div class="item">9</div></div></body></html>
我们重点放在伪类最重要的应用场景:
| 场景 | 描述 |
|---|---|
| 结构伪类 | 根据子元素的位置特征进行选择 |
| 表单伪类 | 根据表单控件状态特征进行选择 |
| 序号 | 选择器 | 描述 | 举例 |
|---|---|---|---|
| 1 | :first-child |
匹配第一个子元素 | div :first-child |
| 2 | :last-child |
匹配最后一个子元素 | div :last-child |
| 3 | :only-child |
选择元素的唯一子元素 | div :only-child |
| 4 | :nth-child(n) |
匹配任意位置的子元素 | div :nth-child(n) |
| 5 | :nth-last-child(n) |
匹配倒数任意位置的子元素 | div :nth-last-child(n) |

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>结构伪类: 不分组匹配</title><style>.container {width: 300px;height: 300px;display: grid;grid-template-columns: repeat(3, 1fr);gap: 5px;}/* 类选择器 */.item {font-size: 2rem;background-color: lightskyblue;display: flex;justify-content: center;align-items: center;}/* body , 第一个单元格都 变了 *//* 为了防止递归,应该在具体的父元素上调用伪类 *//* :nth-child(1) === :first-child */.container > :first-child {/* background-color: wheat; */}/* 匹配最后一个 */.container > :last-child {/* background-color: lightpink; */}/* 匹配任何一个 *//* 索引是从1开始计算 */.container > :nth-child(3) {/* background-color: lightgreen; */}/* :nth-child(n) n: 支持表达式 *//* 当n在表达式中的时, 从0开始 */.container > :nth-child(2n) {/* background-color: magenta; */}/* even: 代表偶数 */.container > :nth-child(even) {/* background-color: magenta; */}/* 选择奇数 */.container > :nth-child(2n-1) {/* background-color: lightsalmon; */}/* odd: 代表奇数 */.container > :nth-child(odd) {/* background-color: lightsalmon; */}/* 只选择前三个 *//* n: 0开始 *//* -0 + 3 = 3-1 +3 = 2-2 +3 = 1 */.container > :nth-child(-n + 3) {/* background-color: lightgreen; */}/* 选择倒数第2个 */.container :nth-last-child(2) {/* background-color: lime; */}/* 从第4个开始,选择剩下的所有元素 */.container > :nth-child(n + 4) {background-color: lightgrey;}</style></head><body><div class="container"><div class="item">1</div><div class="item">2</div><div class="item">3</div><div class="item">4</div><div class="item">5</div><div class="item">6</div><div class="item">7</div><div class="item">8</div><div class="item">9</div></div></body></html>
| 序号 | 选择器 | 描述 | 举例 |
|---|---|---|---|
| 1 | :first-of-type |
匹配按类型分组后的第一个子元素 | div :first-of-type |
| 2 | :last-of-type |
匹配按类型分组后的最后一个子元素 | div :last-of-type |
| 3 | :only-of-type |
匹配按类型分组后的唯一子元素 | div :only-of-type |
| 4 | :nth-of-type() |
匹配按类型分组后的任意位置的子元素 | div :nth-of-type(n) |
| 5 | :nth-last-of-type() |
匹配按类型分组后倒数任意位置的子元素 | div :nth-last-of-type(n) |

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>结构伪类: 分组匹配</title><style>.container {width: 300px;height: 300px;display: grid;grid-template-columns: repeat(3, 1fr);gap: 5px;}/* 类选择器 */.item {font-size: 2rem;background-color: lightskyblue;display: flex;justify-content: center;align-items: center;}.container span:first-of-type {background-color: violet;}.container span:last-of-type {background-color: violet;}/* span分组前三个 */.container span:nth-of-type(-n + 3) {background-color: grey;}.container span:nth-last-of-type(-n + 2) {background-color: coral;}</style></head><body><div class="container"><div class="item">1</div><div class="item">2</div><div class="item">3</div><div class="item">4</div><!-- 分为二组 --><span class="item">5</span><span class="item">6</span><span class="item">7</span><span class="item">8</span><span class="item">9</span></div></body></html>
表单伪类:
有效的 enabled
禁用的 disabled
必选项 required

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>表单伪类</title><style>:root {background-color: lightgreen;}input:enabled {background-color: blanchedalmond;}input:disabled {background-color: lightgreen;}/*input:required {*//* background-color: yellow; *//* } */</style></head><body><h3>用户登录</h3><form action="" method="post"><div><label for="email">邮箱:</label><inputtype="email"id="email"name="email"requiredplaceholder="example@email.com"/></div><div><label for="password">密码:</label><inputtype="password"id="password"name="password"requiredplaceholder="不得少于6位"/></div><div><label for="save">保存密码:</label><input type="checkbox" id="save" name="save" checked readonly /></div><div><label for="save_time">保存期限:</label><select name="save_time" id="save_time"><option value="7" selected>7天</option><option value="30">30天</option></select></div><div><input type="hidden" name="login_time" value="登陆时间戳" /></div><div><label for="warning">警告:</label><inputtype="text"id="warning"value="一天内仅允许登录三次"style="border: none;"disabled/></div><script>// js未学,暂时忽略,只须知道功能即可: 自动生成时间戳,填充到表单隐藏域中document.querySelector('[type="hidden"]').value = new Date().getTime();</script></form></body></html>
| 序号 | 选择器 | 描述 |
|---|---|---|
| 1 | :active |
向被激活的元素添加样式 |
| 2 | :focus |
向拥有键盘输入焦点的元素添加样式 |
| 3 | :hover |
当鼠标悬浮在元素上方时,向元素添加样式 |
| 4 | :link |
向未被访问的链接添加样式 |
| 5 | :visited |
向已被访问的链接添加样式 |
| 5 | :root |
根元素,通常是html |
| 5 | :empty |
选择没有任何子元素的元素(含文本节点) |
| 5 | :not() |
排除与选择器参数匹配的元素 |
通过本次课程的学习,较为细致地了解了选择器的基本类型,以及具体的使用方式,而且老师列举了一些易懂的实例帮助我们理解,知识讲解非常详细,主要内容有:
1、简单选择器,其实主要分为两类:元素选择器和属性选择器,其它的都是这两类的特殊情况,最常用的是: 元素选择器, 类选择器, id 选择器,其优先级依次增加;
2、上下文选择器:html 文档,看上去就像一颗倒置的”树”,所以是有层级结构的,每一个元素, 在文档中, 都有自己的位置,即上下文关系,所以, 完全可以根据元素的上下文关系,来获取到它们;
3、伪类选择器:上下文选择器的局限性,例如选择同一个父级下的第二个子元素,就没那么简单,而伪类就正好弥补了上下文选择器的短板, 所以伪类,大多数是基于文档中元素结构的,其优点主要是对元素的选择更精准,还有表达式,使用方式非常灵活. 伪: 本意是假的,不存在的意思, 这里是特指, 不需要在元素上添加额外的属性来获取元素;类: 暗指伪类的级别, 仍然是属于”class”级别, 仍然属于属性选择器范畴,级别高于元素选择器; 结构伪类(不分组匹配:nth-child(n);分组匹配:nth-of-type(n)),根据子元素的位置特征进行选择。- 允许使用表达式来匹配一组元素,表达式中的”n”是从”0”开始计数,且必须写到前面,- “-n”表示获取前面一组元素,正数表示从指定位置获取余下元素;表单伪类,根据表单控件状态特征进行选择,主要包括有效的enabled、禁用的disabled、必选项required;
4、其它伪类,:active 、:focus、:hover、 :link、:visited、:root、:empty、:not()。
在老师的帮助下,通过梳理以上知识点的层级与关联关系,对相关内容有了在理解的基础上的记忆,便于巩固所学内容,希望能如老师所期待,我们都能做选择器的“终结者”。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号