jquery获取第几个元素的方法总结

   使用jquery时经常会遇到,选择器选择一组元素后,需要在这组元素中找到第几个元素。

  jquery中使用eq()方法找到第几个元素或第N个元素,jquery中eq()的使用如下:

  eq() 选择器选取带有指定 index 值的元素。

  index 值从 0 开始,所有第一个元素的 index 值是 0(不是 1)。

  经常与其他元素/选择器一起使用,来选择指定的组中特定序号的元素。

  例子:

$('#test').children().eq(1).css({'display':'inline-block'});

  

  将id为test的元素的第二个子元素样式设置为'display':'inline-block'。

  另一种写法

$(":eq(index)")
如:$("p:eq(1)")

  

  附另一种办法的例子

<script type="text/javascript" src="/jquery-latest.js"></script> 
<script>
$(function(){
 $('a').each(function(i){
  this.onclick=function(){
   alert(i);
   return false;
  };
 });
});
</script>
<a href="">百度</a>
<a href="">google</a>
<a href="http://www.111cn.net">msn</a>
<a href="">qq</a>

  

  或者这样写

<script type="text/javascript" src="jquery-1.1.3.1.js"></script>
    <script type="text/javascript">
    $(function()
    {
      $("a").bind("click",function()
      {
        alert($("a").index(this));
      }
      )
    }
    )
</script>

  

  效果是一样的哦。

原文地址:https://www.cnblogs.com/c1ndy/p/5336637.html