jquery get element index value () example _jquery

Source: Internet
Author: User
jquery Gets the index value of the element indexed () method:

The index () method of jquery searches for a matching element and returns the index value of the corresponding element, counting starting at 0.

If you do not pass an argument to the. Index () method, the return value is the position of the first element in the set of jquery objects relative to its sibling element.
If the parameter is a set of DOM elements or jquery objects, the return value is the position of the passed element relative to the original collection.
If the parameter is a selector, then the return value is the position of the original element relative to the selector matching element. Returns-1 if no matching element is found.
Copy Code code as follows:

<ul>
<li id= "foo" >foo</li>
<li id= "Bar" >bar</li>
<li id= "Baz" >baz</li>
</ul>

$ (' li '). Index (document.getElementById (' Bar ')); 1, passing a DOM object that returns the index position of the object in the original collection
$ (' li '). Index ($ (' #bar ')); 1, passing a jquery object
$ (' li '). Index ($ (' li:gt (0) ')); 1, passing a set of jquery objects that returns the index position of the first element of the object in the original collection
$ (' #bar '). Index (' Li '); 1, pass a selector, return #bar position in all Li
$ (' #bar '). index (); 1, does not pass the parameter, returns the index position of this element in the sibling.

jquery get element index value () example
Copy Code code as follows:

//for level two or three linkage

<div id= "nav" >
<a href= "http://www.51x" uediannao.com/"> Station material </a>
<a href=" http://www.51xuediannao.com/">jquery special effects </a>
<a href= "http://www.51xuediannao.com/" > Lazy host </a>
<a href= "http://www.51xuediannao.com/qd63/" > Front Road </a>
</div>

$ ("#nav a"). Click (function () {

//Four classic usage
var index1 = $ ("#nav a" "). Index (this);
var index2 = $ ("#nav a"). Index ($ (this));
var index3 = $ (this). Index ()
var index3 = $ (this). Index ("a")
Alert (INDEX3);
return false;
});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.