jquery Gets an example of an element index value ()

Source: Internet
Author: User
Tags relative

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.

Index of the first matching element, relative to the sibling element

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

Grammar

$ (selector). Index ()

Try

The code is as follows Copy Code

<script type= "Text/javascript" src= "/jquery/jquery.js" ></script>
<script type= "Text/javascript" >
$ (document). Ready (function () {
$ ("Li"). Click (function () {
Alert ($ (this). index ());
});
});
</script>
<body>
<p> Click the list item to get its index position relative to the sibling element:</p>
<ul>
<li>Coffee</li>
<li>Milk</li>
<li>Soda</li>
</ul>
</body>


Cases

The code is as follows Copy Code

<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


For two-or three-level linkage
<div id= "NAV" >
<a href= "http://www.111cn.net/" > Construction Station material </a>
<a href= "http://www.111cn.net/" >jquery special effects </a>
<a href= "http://www.111cn.net/" > Script Academy </a>
<a href= "http://www.111cn.net/school/" > website Programming </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;
});

Application examples

Ideas:

When the page section is in the current state, the active style is added.

By obtaining the index value of Li class= "active", the corresponding picture description information is found, which is displayed.

Solve:

The result can be easily achieved by using the index () of jquery.

The code is as follows Copy Code

<div id= "Carousel" >
<div id= "Carouselimg" >
<div id= "Imgcontainer" >
<a href= "#" mce_href= "#" ></a>
<a href= "#" mce_href= "#" ></a>
<a href= "#" mce_href= "#" ></a>
<a href= "#" mce_href= "#" ></a>
<a href= "#" mce_href= "#" ></a>
</div>
</div>
<div id= "Carouseltitle" >
<div class= "Carouseltext" >
<span>&nbsp;</span>
<span>&nbsp;</span>
<span>&nbsp;</span>
<span>&nbsp;</span>
<span>&nbsp;</span>
</div>
<ul>
<li><span>1</span></li>
<li><span>2</span></li>
<li><span>3</span></li>
<li><span>4</span></li>
<li><span>5</span></li>
</ul>
</div>
</div>

Jquery

<script src= "Http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" mce_src= "http:// Ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js "></SCRIPT>
<script type=text/javascript>
var carousedata = [
{index:0,link: "Http://www.111cn.net", Imgsrc: "1.jpg", Text: "Thousands of famous mobile phone 60 percent"},
{index:1,link: "http://www.baidu.com", Imgsrc: "2.jpg", Text: "Test text 2"},
{index:2,link: "Http://www.111cn.net", Imgsrc: "3.jpg", Text: "Test text 3"},
{index:3,link: "http://www.soso.com", Imgsrc: "Xf.jpg", Text: "Test text 4"},
{index:4,link: "Http://www.111cn.net", Imgsrc: "Py.jpg", Text: "Test text 5"}
];


$ (document). Ready (function () {
$ ("#imgcontainer a"). each (function (i) {
$ (this). attr ("href", carousedata[i].link);
$ (this). Children ("img"). attr ("src", carousedata[i].imgsrc);
});

$ (". Carouseltext span"). each (function (i) {
$ (this). Text (Carousedata[i].text);
})

SetInterval (function () {
var Li_index = $ ("#carouseltitle ul li"). Index ($ ("#carouseltitle ul li.active") [0]);
$ (". Carouseltext span"). Hide ();
$ (". Carouseltext span"). EQ (Li_index). Show ();
},10);
});
</script>

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.