Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery:如何获取列表元素的索引_Javascript_Jquery - Fatal编程技术网

Javascript jQuery:如何获取列表元素的索引

Javascript jQuery:如何获取列表元素的索引,javascript,jquery,Javascript,Jquery,给定以下HTML: <ul> <li class="imgThumbLi"> <img src="larry.jpg"/> </li> <li class="imgThumbLi"> <img src="curly.jpg"/> </li> <li class="imgThumbLi"> <img src="

给定以下HTML:

<ul>
    <li class="imgThumbLi">
        <img src="larry.jpg"/>
    </li>
    <li class="imgThumbLi">
        <img src="curly.jpg"/>
    </li>
    <li class="imgThumbLi">
        <img src="moe.jpg"/>
    </li>
</ul>
然后,每当有人将鼠标悬停在其中一个
的“current”(我们正在悬停的)上时,我就会处理一些jQuery
//over)图像的父级
  • 作为一个从零开始的索引,我们处于 //关于
      。 var currListElementIndex=???? } } );
  • 因此,如果用户将鼠标悬停在
    larry.jpg
    上,则
    currListElementIndex
    的值将为
    0
    。如果将鼠标悬停在
    moe.jpg
    上,则值将为
    2
    ,等等。请提前感谢


    编辑:由于其他一些限制,我无法将ID添加到
  • 元素或执行任何其他明显的操作…我需要以某种方式(可能通过父函数???)获取
      ,并确定我在该
        的哪个索引中。

        因为live函数应用于“li”,您可以使用$(this.index())获取其索引

        var currListElementIndex=$(this).index()
        返回索引

        $(".imgThumbLi").live({
            mouseenter:
                function() {
                    // Obtain the source of the current image we're hovering over.
                    var src = $(this).children('img')[0].src;
        
                    // Now I need to know which <li> the "current" (one we're hovering
                    // over) image's parent <li> we're in, as a zero-based  index
                    // of the <ul>.
                    var currListElementIndex = ????
                }
            }
        );
        
        $(".imgThumbLi").live({
            mouseenter:
                function() {          
                    var item=$(this);
                    var src = $(this).children('img')[0].src;
                    var currListElementIndex = item.index();
                    alert(currListElementIndex);
                }
            }
        );