Javascript jQuery:如何获取列表元素的索引
给定以下HTML: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="
<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);
}
}
);