[英]Get the array index of a jQuery selector
I have a collection of buttons, for which I am selecting using class name: 我有一组按钮,可以使用类名进行选择:
<button class="validator">One</button>
<button class="validator">Two</button>
<button class="validator">Three</button>
<button class="validator">Four</button>
<button class="validator">Five</button>
I know that I have 5 buttons in jQuery's array, because: 我知道我在jQuery的数组中有5个按钮,因为:
$(".validator").length;
// returns 5
Say for example I click button "Three", how do I get it's index in the array (which in this instance would be 2) ? 假设我单击按钮“三”,如何获取它在数组中的索引(在本例中为2)?
$('button').on('click', function() { console.log($(this).index()); });
Try this 尝试这个
jQuery(function(){
jQuery('.validator').click(function(i){
console.log(jQuery(this).index());
});
})
JS Fiddle: http://jsfiddle.net/X74Qw/ JS小提琴: http : //jsfiddle.net/X74Qw/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.