jQuery 遍历 - eq() 和siblings() 方法
2014-10-21 20:34
288 查看
eq() 方法将匹配元素集缩减值指定 index 上的一个。
通过为 index 为 2 的 div 添加适当的类,将其变为蓝色:
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>
通过为 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 遍历 - eq() 和siblings() 方法
- jQuery 遍历 - siblings() 方法
- jQuery 遍历 - eq() 方法 查找当前元素
- jQuery基础教程之DOM操作-遍历节点-siblings()方法
- CSS优先级问题以及jQuery中的.eq()遍历方法和:eq()选择器的差别
- jQuery 遍历 - siblings() 方法
- 浅谈jQuery中对象遍历.eq().first().last().slice()方法
- jQuery 遍历 - siblings() 方法匹配集合中每个元素的同胞
- jQuery 遍历 - 过滤---first--last---eq--not--filter方法(二十五)
- jQuery 遍历 - siblings() 方法
- jQuery 遍历 - siblings() 方法
- jQuery 遍历 中 eq() 方法
- jquery循环遍历中add\each\andSelf\eq\filter\map\slice方法的使用
- 浅谈jQuery中对象遍历.eq().first().last().slice()方法
- jQuery 遍历 - 同层方法siblings() next() nextAll() nextUntil() prev() prevAll() prevUntil()(二十四)
- jQuery基础教程之遍历节点siblings()方法使用说明
- jquery遍历json的方法
- jQuery基础教程之DOM操作-遍历节点-prev()方法
- jQuery基础教程之DOM操作-遍历节点-parents()方法
- jQuery,eq(),siblings()