jquery find element's position

两盒软妹~` 提交于 2019-12-21 20:48:26

问题


<ul>
   <li>one</li>
   <li>element</li>
   <li>text</li>
   <li>val</li>
</ul>

how can i get the position in the ul of the clicked li ?


回答1:


I think this will do it for you:

$("li").click(function () {

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

});

Note that the index() function returns the index of the item in the jquery collection. If you have multiple lists on the page, make sure your selector only selects the list items that you want.




回答2:


$('ul li').click(function() {
    alert(   $(this).parent().find('li').index(this)  );
});

Reference

I only tested with one <ul>. You'll need to .each if you're doing this on multiple <ul>s.




回答3:


you can do it with index() http://docs.jquery.com/Core/index



来源:https://stackoverflow.com/questions/2096212/jquery-find-elements-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!