获取点击的列表项的位置 [英] Get the position of the clicked list item

查看:145
本文介绍了获取点击的列表项的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我们有这个标记:

 < header> 
< nav>
< ul>
< li>< a href => first(not)< / a>< / li>
< li>< a href => second< / a>< / li>
< li>< a href => third< / a>< / li>
<! - ...更多在这里 - >
< / ul>
< / nav>
< / header>

我发现有关。index()函数



我试着这样:



on('click','header:not(li:first-child a)a',function(e){
e .preventDefault();
alert($(this).parents('ul')。find('li')。index(this));
});

但它会反映警报: -1



我缺少什么?

解决方案

尽量让父母:

  alert($(this).parent()。index()); 


lets say we have this markup:

<header>
   <nav>
      <ul>
         <li><a href="">first (not)</a></li>
         <li><a href="">second</a></li>
         <li><a href="">third</a></li>
        <!-- ... more here -->
      </ul>
   </nav>
</header>

I found out about .index() function

I'm trying like this:

$('body').on('click','header :not(li:first-child a) a',function(e){
     e.preventDefault();
     alert($(this).parents('ul').find('li').index(this));
});

but it would allways alert: -1

what am I missing?

解决方案

Try to get just parent:

alert( $( this ).parent().index() );

这篇关于获取点击的列表项的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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