English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The index() method returns the index position of the specified element relative to other specified elements.
If no parameters are passed, the return value is an integer indicating the position of the first element relative to its同级 elements.
If a parameter is passed, the index() method will return an integer indicating the position of the first element relative to the selector.
Note:If the element is not found, the index() method will return-1.
Get the index position of the first matching selected element relative to its peers:
$(selector).index()
Get the index position of the element relative to the selector:
$(selector).index(element)
Get the index of the clicked paragraph relative to its peers:
$("p").click(function(){ alert($(this).index()); });Test and see‹/›
Return the index of the ID bar element:
$(document).ready(function(){ let listItem = $("#bar"); $("p").html("Index: ", + $("li").index(listItem)); });Test and see‹/›
Parameter | Description |
---|---|
element | (Optional) Specify the element or selector to get the index position of |