如何在 jQuery 中查找当前选定对象的所有兄弟元素?
jqueryweb developmentfront end technology
要在 jQuery 中查找当前选定对象的所有兄弟元素,请使用 siblings() 方法。siblings([selector]) 方法获取一组元素,其中包含每个匹配元素集合的所有唯一兄弟元素。
以下是此方法 − 的所有参数的描述。
- selector − 这是一个可选的选择器,用于筛选兄弟元素。
示例
您可以尝试运行以下代码来了解如何查找所有兄弟元素:
<html>
<head>
<title>jQuery siblings() method</title>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").siblings('.selected').addClass("highlight");
});
</script>
<style>
.highlight {
background:yellow;
}
</style>
</head>
<body>
<div><span>Hello</span></div>
<p class = "selected">Hello Again</p>
<p>And Again</p>
</body>
</html>

