您的位置:首页 > Web前端 > JQuery

jQuery 遍历 - eq() 和siblings() 方法

2014-10-21 20:34 288 查看
eq() 方法将匹配元素集缩减值指定 index 上的一个。

通过为 index 为 2 的 div 添加适当的类,将其变为蓝色:

<!DOCTYPE html>
<html>
<head>
<style>
div { width:60px; height:60px; margin:10px; float:left;
border:2px solid blue; }
.blue { background:blue; }
</style>
<script type="text/javascript" src="/jquery/jquery.js"></script>
</head>

<body>
<div></div>
<div></div>
<div></div>

<div></div>
<div></div>
<div></div>

<script>$("body").find("div").eq(2).addClass("blue");</script>
</body>
</html>

siblings()查找每个 p 元素的所有类名为 "selected" 的所有同胞元素:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
</head>

<body>
<div><span>Hello</span></div>
<p class="selected">Hello Again</p>
<p>And Again</p>

<script>
$("p").siblings(".selected").css("background", "yellow");
</script>

</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jquery