批改状态:合格
老师批语:能跟上节奏就好
| 序号 | 选择器 | 描述 | 举例 |
|---|---|---|---|
| 1 | 元素选择器 | 根据元素标签名称进行匹配 | div {...} |
| 2 | 群组选择器 | 同时选择多个不同类型的元素 | h1,h2,h3{...} |
| 3 | 通配选择器 | 选择全部元素,不区分类型 | * {...} |
| 4 | 属性选择器 | 根据元素属性进行匹配 | *[...] |
| 5 | 类选择器 | 根据元素 class 属性进行匹配 | *.active {...} |
| 6 | id 选择器 | 根据元素 id 属性进行匹配 | *#top {...} |
当 class,id 选择器不限定被修改的元素类型时, 星号”*“可以省略
ps:寻找元素最简是用标签,id 和class也都属性选择器
/ 标签<class属性<id属性<div&id<class&id /
<!DOCTYPE html><html lang="zh-CN"><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: lightgreen;display: flex;justify-content: center;align-items: center;}body {background-color: lightskyblue;}/* id选择器 */#five {background-color: white;}/* 多个类选择器 */.item.center {background-color: red;}/* id的优先级高于class *//* class&id 选择器不限定被修改的元素类型时, 星号"`*`"可以省略 */.item#five {background-color: yellow;}div#five {background-color: blue;}.item[title] {background-color: pink;}/* 群组选择器 */#first.item,.item.center,.item[title="world"] {background-color: black;color: white;}/* 标签<class属性<id属性<div&id<class&id */</style></head><body><!-- .container>.item{$}*9 快速输入缩写 --><div class="container"><div class="item" id="first">1</div><div class="item" title="hello">2</div><div class="item" title="world">3</div><div class="item">4</div><div class="item center" id="five">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 | 祖先元素 | 拥有子元素,孙元素等所有层级的后代元素 |
| 2 | 父级元素 | 仅拥有子元素层级的元素 |
| 3 | 后代元素 | 与其它层级元素一起拥有共同祖先元素 |
| 4 | 子元素 | 与其它同级元素一起拥有共同父级元素 |
| 序号 | 选择器 | 操作符 | 描述 | 举例 |
|---|---|---|---|---|
| 1 | 后代选择器 | 空格 |
选择当前元素的所有后代元素 | div p, body * |
| 2 | 父子选择器 | > |
选择当前元素的所有子元素 | div > h2 |
| 3 | 同级相邻选择器 | + |
选择拥有共同父级且相邻的元素 | li.red + li |
| 4 | 同级所有选择器 | ~ |
选择拥有共同父级的后续所有元素 | li.red ~ li |
<!DOCTYPE html><html lang="zh-CN"><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: lightgreen;display: flex;justify-content: center;align-items: center;}/* 后代选择器 */.container div {border: 1px solid blue;}/* 父子选择器 */body > div {border: 3px solid pink;}/* 后代选择器模拟父子选择器 */body div.container {border: 5px solid chocolate;}/* 同级相邻选择器 */.item.center + .item {background-color: lightblue;}/* 同级所有选择器 (会选定后面所有同级项目)*/.item.center ~ .item {background-color: lightblue;}</style></head><body><!-- .container>.item{$}*9 快速输入缩写 --><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="zh-CN"><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: lightgreen;display: flex;justify-content: center;align-items: center;}/* 为了防止递归,应该在具体的父元素上调用伪类选择器 */.container > :first-child {background-color: rosybrown;}/* 匹配最后一个 */.container > :last-child {background-color: slateblue;}/* 匹配容器中任何一个,索引是从第一个开始计算 */.container > :nth-child(6) {background-color: tomato;}/* 匹配倒数第n个,使用nth-last-child(n),索引从最后一个倒着开始计算 */.container > :nth-last-child(2) {background-color: yellow;}/* :nth-child()支持表达式,如选中所有奇数item用odd或者(2n-1),偶数用2n或者even */.container > :nth-child(2n - 1) {background-color: lightskyblue;}/* 只选择前三个 (-n+3)*/.container > :nth-child(-n + 3) {background-color: green;}/* 选择第5个开始开始所有元素 */.container > :nth-child(n + 5) {border: indigo 5px solid;}</style></head><body><!-- .container>.item{$}*9 快速输入缩写 --><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) |
“-n”表示获取前面一组元素,正数表示从指定位置获取余下元素
<!DOCTYPE html><html lang="zh-CN"><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: lightgreen;display: flex;justify-content: center;align-items: center;}/* 匹配分组中的第一个 */.container > span:first-of-type {background-color: green;}/* 匹配分组中的最后一个 */.container span:last-of-type {background-color: yellow;}/* 匹配分组中的第二个 */.container span:nth-of-type(2) {background-color: hotpink;}/* 匹配分组中第3个之后所有 */.container span:nth-of-type(n + 3) {background-color: hotpink;}/* 同等优先级,最后一个代码有效 */.container span:last-of-type {background-color: yellow;}</style></head><body><!-- .container>.item{$}*9 快速输入缩写 --><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>
**表单选择器
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>表单选择器</title><style>input:enabled {background-color: khaki;}input:disabled {background-color: gray;color: black;}input:required {border: blueviolet 1px solid;}</style></head><body><h2>用户登陆</h2><form action=""><div><label for="E-mail">邮箱:</label><input type="email" id="E-mail" placeholder="example@qq.com" required /></div><div><label for="password">密码:</label><inputtype="password"id="password"name="password"placeholder="不少于6位"minlength="6"required/></div><div><label>是否保存密码:</label><input type="checkbox" name="cunchu" /></div><div><label for="warning">警告:</label><inputtype="text"id="warning"value="一天内仅允许登陆1次"style="border:none;"disabled/></div><button>提交</button></form></body></html>
| 序号 | 选择器 | 描述 |
| —— | ————— | ——————————————————— |
| 1 | :active | 向被激活的元素添加样式 |
| 2 | :focus | 向拥有键盘输入焦点的元素添加样式 |
| 3 | :hover | 当鼠标悬浮在元素上方时,向元素添加样式 |
| 4 | :link | 向未被访问的链接添加样式 |
| 5 | :visited | 向已被访问的链接添加样式 |
| 5 | :root | 根元素,通常是html |
| 5 | :empty | 选择没有任何子元素的元素(含文本节点) |
| 5 | :not() | 排除与选择器参数匹配的元素 |
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>其他伪类选择器</title><style>a:link {color: coral;}a:visited {color: green;}a:hover {color: blue;}a:active {color: rgb(255, 7, 7);}</style></head><body><h2>超级链接4中伪类选择</h2><div><a href="https://www.php.cn" target="_blank">php中文网</a></div><div><a href="https://www.php.cn" target="_blank">我也是php中文网</a></div></body></html>
总结:因为一直没时间看直播,前几天都是直接2倍看录播,完了在回头敲,结果好多时候没时间就只是看了一遍录播,记得效果不好,好多有印象,但是敲不出来。昨天看群里有同学说一边看一边敲,看完也就写完了,今天也是这样做的,虽然时间比较可是效果还是不错的。今年内容理解的最好的一次。其他类的还有几个没完全尝试完,但因为时间问题,明天再做研究。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号