How to get the index number of jquery

When using jquery, it is often encountered that after a selector selects a group of elements, it is necessary to find the number of elements in this group of elements.

Use the eq() method in jquery to find the number of elements or the Nth element. The use of eq() in jquery is as follows:

The eq() selector selects the element with the specified index value.

The index value starts from 0, and the index value of all first elements is 0 (not 1).

Often used with other elements/selectors to select an element with a specific ordinal number in a specified group.

1

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

Set the style of the second child element of the element with id test to 'display':'inline-block'.

another way of writing

1

$(":eq(index)")

Such as: $("p:eq(1)")

An example of another approach

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>

or write like this

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>

The effect is the same.

Guess you like

Origin blog.csdn.net/winkexin/article/details/131017222