jquery获取第几个索引方法

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

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

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

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

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

1

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

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

另一种写法

1

$(":eq(index)")

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

附另一种办法的例子

1

2

3

4

5

6

7

8

9

10

11

<script type="text/javascript" src="/jquery-latest.js"></script>

<script>

$(function(){

$('a').each(function(i){

this.onclick=function(){

alert(i);

return false;

};

});

});

</script>

或者这样写

1

2

3

4

5

6

7

8

9

10

11

12

<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>

效果是一样的哦。

猜你喜欢

转载自blog.csdn.net/winkexin/article/details/131017222