How can I check the number of the item that was clicked
Let's say I have many items with click event (all have same element name and possibly same class).
<a>开发者_开发问答A</a>
<a>B</a>
<a>C</a>
<a>D</a>
then I have method that is triggered on click event. How can I check number of the item that was clicked?
In the example if 'C' was clicked then I should get 3 as an answer.
You can use .prevAll()
:
$('a').click(function() {
alert($(this).prevAll('a').length + 1);
});
Or .index()
:
$('a').click(function() {
alert($(this).index() + 1);
});
You can also use .index()
$("a").bind("click",function(){
alert($(this).index()); // 0 indicates first item
});
精彩评论