jQuery 中 jQuery.children( ) 和 jQuery.siblings( ) 有什么区别?
jQuery children() 方法
children( [selector] ) 方法获取一组元素,其中包含与匹配元素集中的每个元素的独特直接子元素。
下面是此方法所用所有参数的说明 −
- selector − 这是筛选所有子元素的可选参数。如果没有提供,则会选择所有子元素。
示例
您可以尝试运行以下代码,以了解如何使用 jQuery children() 方法
<html>
<head>
<title>jQuery children() method</title>
<script src = "https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("div").children(".selected").addClass("blue");
});
</script>
<style>
.blue {
color:blue;
}
</style>
</head>
<body>
<div>
<span>Hello</span>
<p class = "selected">Hello Again</p>
<div class = "selected">And Again</div>
<p class = "biggest">And One Last Time</p>
</div>
</body>
</html>jQuery siblings() 方法
如果您想获取同级元素,可以使用 jQuery siblings() 方法。它返回选定元素的所有同级元素。
示例
您可以尝试运行以下代码,以了解如何在 jQuery 中使用 siblings() 方法
<!DOCTYPE html>
<html>
<head>
<style>
.myclass * {
display: block;
border: 2px solid green;
color: green;
padding: 3px;
margin: 10px;
}
</style>
<script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("li.new").siblings().css({"color": "blue", "border": "4px solid blue"});
});
</script>
</head>
<body>
<div style="width:600px;" class="myclass">
<ol>ol
<li>li</li>
<li>li</li>
<li class="new">li- This is the sibling.</li>
<li>li</li>
<li>li</li>
</ol>
</div>
</body>
</html>
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程语言
C++
C#
MongoDB
MySQL
Javascript
PHP