Returns -1 if the object wasn't found.
Element subject : Object to search for.
Example:
On click, returns the index (based zero) of that div in the page.
$("div").click(function () {
// this is the dom element clicked
var index = $("div").index(this);
$("span").text("That was div index #" + index);
});
<span>Click a div!</span>
<div>First div</div>
<div>Second div</div>
<div>Third div</div>
div { background:yellow; margin:5px; }
span { color:red; }
Example:
Returns the index for the element with ID foobar.
$("*").index( $('#foobar')[0] )
<div id="foobar"><b></b><span id="foo"></span></div>
Example:
Returns the index for the element with ID foo within another element.
$("*").index( $('#foo')[0] )
<div id="foobar"><b></b><span id="foo"></span></div>
Example:
Returns the index for the element clicked within a collection.
var mainNavLinks = $('ul#mainNav li a');
mainNavLinks.click(function(){alert(mainNavLinks.index(this)0;});
Example:
Returns -1, as there is no element with ID bar.
$("*").index( $('#bar')[0] )
<div id="foobar"><b></b><span id="foo"></span></div>
No comments:
Post a Comment