The example in this article describes the usage of the index() method in jQuery. Share it with everyone for your reference. The specific analysis is as follows:
This method can search for matching elements and return the index value of the element.
Index values start from 0.
Grammar structure 1:
When this method has no parameters, the return value is the index position of the specified element in the collection of its sibling elements.
Example code:
Example 1:
The above code can return the index value of the li element with the class name qian in its set of sibling elements. Seeing this, you may have questions about whether the so-called sibling elements are similar elements, that is to say, the li element is in the li element. The index value in the collection.
Example 2:
As you can see from the above code, it is not just elements of the same type, but all elements of the same generation.
Grammar structure 2:
When the parameter of the method is a DOM object or jQuery object, the return value is the index of this DOM object or jQuery object in the specified element collection.
If the specified DOM object or jQuery object is not found in the specified element collection, the return value is -1.
Parameter list:
Example code:
Example 1:
Example 2:
Because no matching element is found, the return value is -1.
Grammar structure three:
When the parameter of the method is a selector, the element will be found from the collection of objects obtained through this selector.
Parameter list:
Example code:
The above code will obtain the index position of the li element with the id value qian in the collection of li objects obtained through the li selector.
I hope this article will be helpful to everyone’s jQuery programming.