jQuerychildren()方法
children([selector])方法获取一组元素,其中包含每个匹配元素集的所有唯一直接子元素。
这是此方法使用的所有参数的描述-
选择器 -这是一个可选参数,用于过滤掉所有子项。如果未提供,则将选择所有子项。
您可以尝试运行以下代码来学习如何使用jQuerychildren()方法:
<html>
<head>
<title>jQuery children() method</title>
<script src = "https://cdn.staticfile.org/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>jQuerysiblings()方法
如果要获取同级元素,请使用jQuerysiblings()方法。它返回所选元素的所有同级元素。
您可以尝试运行以下代码来学习如何siblings()在jQuery中使用方法:
<!DOCTYPE html>
<html>
<head>
<style>
.myclass * {
display: block;
border: 2px solid green;
color: green;
padding: 3px;
margin: 10px;
}
</style>
<script src="https://cdn.staticfile.org/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>