jQuery Miscellaneousindex()Methods

jQuery 杂项方法jQuery Miscellaneous Methods

Examples

Get the index of the clicked <li> element relative to its sibling elements:

$("li").click(function(){
alert($(this).index());
});

Try it Yourself »

Definition and Usage

The index() method returns the index position of a specified element relative to other specified elements.

These elements can be specified via jQuery selectors or DOM elements.

Note:If the element is not found, index() will return -1.


The index of the first matching element relative to its sibling elements.

Get the index position of the first matching element relative to its sibling elements.

Syntax

$(selector).index()

Try it Yourself »


The index of the element relative to the selector.

Get the index position of an element relative to a selector.

The element can be specified via a DOM element or a jQuery selector.

Syntax

$(selector).index(element)

Try it Yourself »

Parameters Description
element Optional. Specifies the element for which to obtain the index position. Can be a DOM element or a jQuery selector.


jQuery 杂项方法jQuery Miscellaneous Methods

Other Extensions