siblings([expr])
得到所有匹配元素集合中各个元素的所有兄弟元素集合。返回匹配元素集合
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
var len = $(".hilite").siblings()
.css("color", "red")
.length;
$("b").text(len);
});
</script>
<style>
ul { float:left; margin:5px; font-size:16px; font-weight:bold; }
p { color:blue; margin:10px 20px; font-size:16px; padding:5px;
font-weight:bolder; }
.hilite { background:yellow; }
</style>
</head>
<body>
<ul>
<li>One</li>
<li>Two</li>
<li class="hilite">Three</li>
<li>Four</li>
</ul>
<ul>
<li>Five</li>
<li>Six</li>
<li>Seven</li>
</ul>
<ul>
<li>Eight</li>
<li class="hilite">Nine</li>
<li>Ten</li>
<li class="hilite">Eleven</li>
</ul>
<p>Unique siblings: <b></b></p>
</body>
</html>
$(".hilite").siblings()
将得到包含特殊样式‘hilite’元素的所有兄弟元素集合。以下是匹配元素集合
<li>One</li>
<li>Two</li>
<li>Four</li>
<li>Eight</li>
<li class="hilite">Nine</li>
<li>Ten</li>
<li class="hilite">Eleven</li>
本文介绍如何使用jQuery的siblings()方法来获取DOM中带有特定类名元素的所有兄弟元素,并通过一个实例展示了如何改变这些兄弟元素的颜色并计算它们的数量。
245

被折叠的 条评论
为什么被折叠?



