找到点击的li号码 [英] Find the clicked li number

查看:66
本文介绍了找到点击的li号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标准清单。

<ul>
  <li><a href="#">blah 1</a></li>
  <li><a href="#">blah 2</a></li>
  <li><a href="#">blah 3</a></li>
  <li><a href="#">blah 4</a></li>
</ul>

我的jQuery:

$('ul li a').live('click', function() {
  var parent = $(this).parent('li');
});

我想知道的是父母李先生在点击链接的列表,例如点击blah 3会给我2,等等4会给3等。

What I want to find out is the parent li's position in the list of the clicked link e.g. clicking on blah 3 would give me 2, blah 4 would give 3 etc.

推荐答案

$('ul li a').live('click', function() {
    console.log($(this).parent('li').index());
});

会给你你想要的东西,但要记住这些是基于0的索引 - 即第一个订单项为索引0,最后一个订单项为3.

Will give you what you want, but keep in mind these are 0 based indexes -- ie the first line item is index 0, the last line item is 3.

jQuery index()方法文档

这篇关于找到点击的li号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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