使用 jQuery 获取集合中单击元素的索引 [英] Get index of clicked element in collection with jQuery

查看:33
本文介绍了使用 jQuery 获取集合中单击元素的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取下面代码中点击项目的索引?

How do I get the index of clicked item in the code below?

$('selector').click(function (event) {
    // get index in collection of the clicked item ...
});

使用 Firebug 我看到了这个:jQuery151017197709735298827: 2(我点击了第二个元素).

With Firebug I saw this: jQuery151017197709735298827: 2 (I've clicked in the second element).

推荐答案

这将提醒点击选择器的索引(第一个从 0 开始):

This will alert the index of the clicked selector (starting with 0 for the first):

$('selector').click(function(){
    alert( $('selector').index(this) );
});

这篇关于使用 jQuery 获取集合中单击元素的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆