语法
https://api.jquery.com/category/selectors/hierarchy-selectors/
- 获得A元素内部的所有的B元素:
$('A B')
— 后代选择器(包含子孙) - 获得A元素下面的所有的B子元素:
$('A > B')
— 只有儿子,没有孙子 - 获得A元素同级下一个B元素:
$('A + B')
— 下一个兄弟 - 获得A元素同级所有后面B元素:
$('A ~ B')
— 后边的所有兄弟 - 获得A元素的同级B元素:
$('A').siblings('B')
— 获取所有的兄弟(包含前面的和后面的)
示例
后代选择器(包含子孙)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="js/jquery-3.7.1.js"></script>
</head>
<body>
<input type="button" id="b1" value="点击按钮,改变<body>内所有<div>的背景色为红色"><br><br>
<div id="one" style="width: 150px;height: 150px; background-color: #979ccd;">id为one</div>
<div id="two" style="width: 150px; height: 150px; background-color: skyblue;">
id为two
<div class="mini" style="width: 50px;height: 50px;background-color: blueviolet;">class为mini</div>
</div>
<span style="display: inline-block; width: 150px;height: 150px;background-color: purple;">span</span>
<script>
$('#b1').click(function () {
$('body div').css('background-color', 'red')
})
</script>
</body>
</html>
点击按钮:
儿子选择器,没有孙子
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="js/jquery-3.7.1.js"></script>
</head>
<body>
<input type="button" id="b1" value="点击按钮,改变<body>内的子<div>的背景色为红色"><br><br>
<div id="one" style="width: 150px;height: 150px; background-color: #979ccd;">id为one</div>
<div id="two" style="width: 150px; height: 150px; background-color: skyblue;">
id为two
<div class="mini" style="width: 50px;height: 50px;background-color: blueviolet;">class为mini</div>
</div>
<span style="display: inline-block; width: 150px;height: 150px;background-color: purple;">span</span>
<script>
$('#b1').click(function () {
$('body > div').css('background-color', 'red')
})
</script>
</body>
</html>
点击按钮:
下一个兄弟
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="js/jquery-3.7.1.js"></script>
<style>
#one {
float: left;
width: 150px;
height: 150px;
background-color: #979ccd;
}
#two {
margin-left: 20px;
float: left;
width: 150px;
height: 150px;
background-color: skyblue;
}
.mini {
margin-top: 10px;
width: 50px;
height: 50px;
background-color: blueviolet;
}
.one {
float: left;
margin-left: 20px;
width: 150px;
height: 150px;
background-color: #979ccd;
}
span {
display: inline-block;
margin-left: 20px;
width: 150px;
height: 150px;
background-color: purple;
}
</style>
</head>
<body>
<input type="button" id="b1" value="点击按钮,改变id为one的下一个<div>的背景色为红色"><br><br>
<div id="one">id为one</div>
<div id="two">id为two
<div class="mini">class为mini</div>
</div>
<div class="one">class是one
<div class="mini">class是mini</div>
<div class="mini">class是mini</div>
</div>
<div class="one">class是one</div>
<span>span</span>
<script>
$('#b1').click(function () {
$('#one + div').css('background-color', 'red')
})
</script>
</body>
</html>
点击按钮:
后边的所有兄弟
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="js/jquery-3.7.1.js"></script>
<style>
#one {
float: left;
width: 150px;
height: 150px;
background-color: #979ccd;
}
#two {
margin-left: 20px;
float: left;
width: 150px;
height: 150px;
background-color: skyblue;
}
.mini {
margin-top: 10px;
width: 50px;
height: 50px;
background-color: blueviolet;
}
.one {
float: left;
margin-left: 20px;
width: 150px;
height: 150px;
background-color: #979ccd;
}
span {
display: inline-block;
margin-left: 20px;
width: 150px;
height: 150px;
background-color: purple;
}
</style>
</head>
<body>
<input type="button" id="b1" value="点击按钮,改变id为two的元素后面的所有兄弟<div>元素的背景色为红色"><br><br>
<div id="one">id为one</div>
<div id="two">id为two
<div class="mini">class为mini</div>
</div>
<div class="one">class是one
<div class="mini">class是mini</div>
<div class="mini">class是mini</div>
</div>
<div class="one">class是one</div>
<span>span</span>
<script>
$('#b1').click(function () {
$('#two ~ div').css('background-color', 'red')
})
</script>
</body>
</html>
点击上面按钮:
获取所有的兄弟(包含前面的和后面的)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="js/jquery-3.7.1.js"></script>
<style>
#one {
float: left;
width: 150px;
height: 150px;
background-color: #979ccd;
}
#two {
margin-left: 20px;
float: left;
width: 150px;
height: 150px;
background-color: skyblue;
}
.mini {
margin-top: 10px;
width: 50px;
height: 50px;
background-color: blueviolet;
}
.one {
float: left;
margin-left: 20px;
width: 150px;
height: 150px;
background-color: #979ccd;
}
span {
display: inline-block;
margin-left: 20px;
width: 150px;
height: 150px;
background-color: purple;
}
</style>
</head>
<body>
<input type="button" id="b1" value="点击按钮,改变id为two的元素所有<div>兄弟元素的背景色为红色"><br><br>
<div id="one">id为one</div>
<div id="two">id为two
<div class="mini">class为mini</div>
</div>
<div class="one">class是one
<div class="mini">class是mini</div>
<div class="mini">class是mini</div>
</div>
<div class="one">class是one</div>
<span>span</span>
<script>
$('#b1').click(function () {
$('#two').siblings('div').css('background-color', 'red')
})
</script>
</body>
</html>
点击上面按钮: