Home > Web Front-end > JS Tutorial > Example of usage of index() method in jQuery_jquery

Example of usage of index() method in jQuery_jquery

WBOY
Release: 2016-05-16 16:24:16
Original
1365 people have browsed it

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.

Copy code The code is as follows:
$(selector).index()

Example code:

Example 1:

Copy code The code is as follows:






index() function-Script Home

Script Home






  • Backstage area

  • Front desk area

  • Database Area

  • Webmaster Communication



The position of the current li element:0




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:

Copy code The code is as follows:






index() function-Script Home

Script Home






  • Backstage area

  • Front desk area

  • Database Area

  • Webmaster Communication

  • Search Optimization


The position of the current li element:0




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.

Copy code The code is as follows:
$(selector).index(element)

Parameter list:

参数 描述
element 获得index位置的DOM对象或者jQuery对象。

Example code:

Example 1:

Copy code The code is as follows:






index() function-Script Home

Script Home






  • Backstage area

  • Front desk area

  • Database Area

  • Webmaster Communication



The position of the current li element:0




Example 2:

Because no matching element is found, the return value is -1.

Copy code The code is as follows:






index() function-Script Home

Script Home






  • Backstage area

  • Front desk area

  • Database Area

  • Webmaster Communication

  • Search Optimization


The position of the current li element:0




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.

Copy code The code is as follows:
$(selector).index(Jqselector)

Parameter list:

参数 描述
Jqselector 选择器,将会从通过此选择器获得的对象中查找元素。

Example code:

Copy code The code is as follows:






index() function-Script Home

Script Home






  • Backstage area

  • Front desk area

  • Database Area

  • Webmaster Communication

  • Search Optimization


The position of the current li element:0




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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template