get count of list element( based upon click)?
PS: I am using this Jquery plugin, although my question is not dependent on that.
<div class="pikachoose">
<ul id="pikame" class="jcarousel-skin-pika">
<li><a href="http://www.开发者_运维问答pikachoose.com"><img src="../../10.jpg"/></a</li>
<li><a href="http://www.pikachoose.com"><img src="../../20.jpg"/></a></li>
<li><a href="http://www.pikachoose.com"><img src="../../30.jpg"/></a</li>
<li><a href="http://www.pikachoose.com"><img src="../../40.jpg"/></a></li>
<li><a href="http://www.pikachoose.com"><img src="../../50.jpg"/></a></li>
</ul>
</div>
I want to get the index of "li" tag clicked.
$("#pikame li").size();
will return me total number of "li". But i want to get index of that "li"
Like if image 40.jpg is clicked then i want to get 3(index starting from 0,1,2 so on...).
$('a').click(function(){
alert($(this).parent().index());
});
Working example: http://jsfiddle.net/AlienWebguy/zvkgv/
EDIT: Expanded example per comment request:
$('#pikame a').click(function(){
$('#pikame2 li').eq($(this).parent().index()).find('img').trigger('click');
return false;
});
New Working example: http://jsfiddle.net/AlienWebguy/zvkgv/2/
I'd use delegate
on the #pikame element, then index
on the li
:
$("#pikame").delegate("li", "click", function() {
// $(this).index() is the index of the `li` clicked
});
Live example
Though it really doesn't matter how you get to the li
element; the key bit is once you're there, index
with no arguments tells you its index amongst its siblings. Since li
elements can only have li
siblings, no need to give index
a selector or anything.
精彩评论