How to get the position of an specific li in a ul tag?

一笑奈何 提交于 2019-12-02 10:41:25

问题


I want to get number of one li that I hover on with jQuery. this is my code :

<ul>
   <li></li>
   <li></li>
   <li></li> /* I want hover on this element */
   <li></li>
   <li></li>
</ul>

I want when hover on certain element in top code get number of all lies.... for example in top code I want get 3 (This means that this third element)


回答1:


You can use this:

var number = $(this).index() + 1;

Demo here

The .index() starts at 0 so I added +1 because you wanted to start counting from 1.



来源:https://stackoverflow.com/questions/18555857/how-to-get-the-position-of-an-specific-li-in-a-ul-tag

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