<ul> <div></div> <li id="foo1">foo</li> <li id="bar1">bar</li> <li id="baz1">baz</li> <div></div> </ul>
var $foo = $( "#foo1" ); console.log( "Index: " + $foo.index() ); // 1 var $listItem = $( "li" ); // 隐式调用.last() console.log( "Index: " + $listItem.index() ); // 3 console.log( "Index: " + $listItem.last().index() ); // 3 var $div = $( "div" ); console.log( "Index: " + $div.index() ); // 4 console.log( "Index: " + $div.last().index() ); // 4 |
<ul> <div class="test"></div> <li id="foo1">foo</li> <li id="bar1" class="test">bar</li> <li id="baz1">baz</li> <div class="test"></div> </ul> <div id="last"></div>
var $foo = $( "li" ); // 隐式调用.first() console.log( "Index: " + $foo.index( "li" ) ); // 0 console.log( "Index: " + $foo.first().index( "li" ) ); // 0 var $baz = $( "#baz1" ); console.log( "Index: " + $baz.index( "li" )); // 2 var $listItem = $( "#bar1" ); console.log( "Index: " + $listItem.index( ".test" ) ); // 1 var $div = $( "#last" ); console.log( "Index: " + $div.index( "div" ) ); // 2 |
<ul> <div class="test"></div> <li id="foo1">foo</li> <li id="bar1" class="test">bar</li> <li id="baz1">baz</li> <div class="test"></div> </ul> <div id="last"></div>
var $foo = $( "li" ); var $baz = $( "#baz1" ); console.log( "Index: " + $foo.index( $baz ) ); // 2 var $tests = $( ".test" ); var $bar = $( "#bar1" ); // 隐式调用参数的.first() console.log( "Index: " + $tests.index( $bar ) ); // 1 console.log( "Index: " + $tests.index( $bar.first() ) ); // 1 |