<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Document</title>
<style>
* {
font-size: 20px;
font-weight: 700;
padding-left: 20px;
margin-top: 20px;
}
.test1 p:nth-child(3),
.test2 p:nth-child(3),
.test3 p:nth-child(3) {
color: darkmagenta;
}
.test1 p:nth-of-type(4),
.test2 p:nth-of-type(4),
.test3 p:nth-of-type(4) {
color: coral;
}
</style>
</head>
<body>
<div class="test1">
<p>第一个p</p>
<p>第二个p</p>
<p>第三个p</p>
<p>第四个p</p>
</div>
<hr>
<div class="test2">
<p>第一个p</p>
<span>插入元素span</span>
<p>第二个p</p>
<p>第三个p</p>
<p>第四个p</p>
</div>
<hr>
<div class="test3">
<p>第一个p</p>
<p>第二个p</p>
<span>插入元素span</span>
<p>第三个p</p>
<p>第四个p</p>
</div>
<div>nth-of-type是取当前元素的兄弟元素的第n个,nth-child取的是当前元素的第n个节点的当前元素</div>
<div>元素:nth-child(n)中,当第n个不是该元素时,则选择器失效,取不到该元素</div>
</body>
</html>