首页 > Web开发 > 详细

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

时间:2014-10-21 21:35:07      阅读:326      评论:0      收藏:0      [点我收藏+]

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 遍历 - eq() 和siblings() 方法

原文:http://blog.csdn.net/gisredevelopment/article/details/40350729

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!