批改状态:合格
老师批语:发给大家的课件, 大家一定要保留好, 是一个很好的资料库
| 序号 | 选择器 | 描述 | 举例 |
|---|---|---|---|
| 1. | 元素选择器 | 根据元素名进行匹配 | p {...} |
| 2. | 类选择器 | 根据class属性进行匹配 |
.active {...} |
| 3. | ID选择器 | 根据id属性进行匹配,一般一个页面只有一个名称的id |
#top |
| 4. | 通配符选择器 | 选择全部元素,不区分类型,一般用于浏览器默认样式重置 | * {...} |
| 5. | 分组选择器 | 同时选择多个不同类型的元素 | h1,h2,h3 {...} |
| 6. | 属性选择器 | 根据元素的属性进行匹配 | input[type="text"] {...} |
其中最常用的是:元素选择器,类选择器,ID选择器.
| 序号 | 角色 | 描述 |
|---|---|---|
| 1. | 祖先元素 | 拥有子元素,孙元素等所有层级的后代元素 |
| 2. | 父级元素 | 仅拥有子元素层级的元素 |
| 3. | 兄弟元素 | 即层次相同的元素 |
| 4. | 后代元素 | 拥有子,孙…等所有更低层级的元素 |
| 5. | 子元素 | 即父级元素下的低一个层级的元素 |
| 序号 | 选择器 | 操作符 | 描述 | 举例 |
|---|---|---|---|---|
| 1. | 后代选择器 | 空格 |
选择当前元素的所有后代元素 | div p {...} |
| 2. | 父子选择器 | > |
选择当前元素的所有子元素 | div > p {...} |
| 3. | 毗邻选择器 | + |
选择当前元素的同父级且相邻的元素 | h1 + p {...} |
| 4. | 毗邻兄弟选择器 | ~ |
选择当前元素的同父级后续的所有元素 | li.red ~ li {...} |
按照应用场景可以对伪类选择器进行如下分类:
| 场景 | 描述 |
|---|---|
| 结构伪类 | 根据子元素的位置特征进行选择 |
| 表单伪类 | 根据表单控件状态特征进行选择 |
| 序号 | 选择器 | 描述 | 举例 |
|---|---|---|---|
| 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) |
| 序号 | 选择器 | 描述 | 举例 |
|---|---|---|---|
| 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) |
| 序号 | 选择器 | 描述 |
|---|---|---|
| 1 | :active |
向被激活的元素添加样式 |
| 2 | :focus |
向拥有键盘输入焦点的元素添加样式 |
| 3 | :hover |
当鼠标悬浮在元素上方时,向元素添加样式 |
| 4 | :link |
向未被访问的链接添加样式 |
| 5 | :visited |
向已被访问的链接添加样式 |
| 5 | :root |
根元素,通常是html |
| 5 | :empty |
选择没有任何子元素的元素(含文本节点) |
| 5 | :not() |
排除与选择器参数匹配的元素 |
<!doctype html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport"content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><style>* {margin: 0;padding: 0;}.container {width: 400px;text-align: center;}h1 {margin-bottom: 10px;color: darkgoldenrod;}ul>li {float: left;list-style-type: none;width: 20px;height: 20px;line-height: 20px;margin: 0 10px;border-radius: 10px;color: #ffffff;text-align: center;}ul li:first-child {background-color: red;}ul li:last-child {background-color: aqua;}ul li:nth-child(2) {background-color: blue;}ul li:nth-child(3),li:nth-child(3)+li {background-color: blueviolet;}#bg-purple {background-color: purple;}.bg-wheat {background-color: wheat;}li[title] {background-color: deeppink;}</style><title>双色球</title></head><body><div class="container"><h1>双色球</h1><ul><li>1</li><li>2</li><li>3</li><li>4</li><li id="bg-purple">5</li><li class="bg-wheat">6</li><li class="bg-wheat">7</li><li title="extra">8</li><li title="extra">9</li><li>10</li></ul></div></body></html>
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><style>table {width: 200px;border-collapse: collapse;text-align: center;}th,td {border: 1px solid;color: #ffffff;}th {background-color: pink;}/* 偶数行 */tr:nth-child(even) {/* background-color: deepskyblue; */}tr:nth-child(2n) {background-color: deepskyblue;}/* 奇数行 */tr:nth-child(odd) {/* background-color: lightskyblue; */}tr:nth-child(2n+1) {background-color: lightskyblue;}</style><title>表格隔行换色</title></head><body><table><caption>表格隔行换色</caption><tr><th>姓名</th><th>性别</th></tr><tr><td>1</td><td>张三</td></tr><tr><td>2</td><td>李四</td></tr><tr><td>3</td><td>王五</td></tr><tr><td>4</td><td>赵六</td></tr></table></body></html>
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><style>a {text-decoration: none;}a:link {color: #000000;}a:visited {color: red;}a:hover {text-decoration: underline;}a:active {background-color: blueviolet;}input:focus {background-color: #ff7300;}/* 根元素通常是html */:root {background-image: url("images/bg.jpeg");}p:empty {width: 40px;height: 20px;background-color: brown;}/* 否定伪类:选择器取反,除了第一个li元素 */li:not(.first) {font-size: 20px;color: chartreuse;}</style><title>表单伪类</title></head><body><a href="http://www.php.cn">PHP中文网</a><!-- 没有内容的p元素 --><p></p><form><input type="text" autofocus></form><ul><li class="first">1</li><li>2</li><li>3</li></ul></body></html>



Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号