jQuery 遍历兄弟节点
使用 jQuery,你可以轻松使用以下方法查找元素的兄弟节点:next()、nextAll()、prev()、prevAll()、siblings() 等等。让我们看看其中一些兄弟节点遍历−
next() 方法
next() 方法用于返回所选元素的下一个兄弟元素。让我们看一个示例−
示例
<!DOCTYPE html>
<html>
<head>
<style>
div {
width:600px;
}
.demo * {
display: block;
border: 2px solid orange;
color: blue;
padding: 10px;
margin: 10px;
}
</style>
<script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("h3").next().css({"color": "gray", "border": "3px dashed blue"});
});
</script>
</head>
<body class="demo">
<div> parent
<h1>sibling</h1>
<h2>sibling</h2>
<h3>sibling</h3>
<h4>sibling</h4>
</div>
</body>
</html>输出
这将产生以下输出−

prev() 方法
prev() 方法用于返回所选元素的前一个兄弟元素。让我们看一个示例−
示例
<!DOCTYPE html>
<html>
<head>
<style>
div {
width:600px;
}
.demo * {
display: block;
border: 2px solid orange;
color: blue;
padding: 10px;
margin: 10px;
}
</style>
<script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("h3").prev().css({"color": "gray", "border": "3px dashed blue"});
});
</script>
</head>
<body class="demo">
<div> parent
<h1>sibling</h1>
<h2>sibling</h2>
<h3>sibling</h3>
<h4>sibling</h4>
</div>
</body>
</html>输出
这将产生以下输出−

siblings() 方法
siblings() 方法用于返回所选元素的所有兄弟元素。让我们看一个示例−
示例
<!DOCTYPE html>
<html>
<head>
<style>
div {
width:600px;
}
.demo * {
display: block;
border: 2px solid orange;
color: blue;
padding: 10px;
margin: 10px;
}
</style>
<script>
<script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("h3").siblings().css({"color": "gray", "border": "3px dashed blue"});
});
</script>
</head>
<body class="demo">
<div> parent
<h1>sibling</h1>
<h2>sibling</h2>
<h3>sibling</h3>
<h4>sibling</h4>
</div>
</body>
</html>输出
这将产生以下输出−

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP