我有一个包含12个链接(图像)的图库,当点击打开灯箱并显示点击的图像时。但是当我点击图像时,它总是打开最后一个。当我尝试控制日志索引时,我总是得到" -1&#34 ;;
var image= $('.Gallery div a');
console.log(image.index($(e.target)));
答案 0 :(得分:1)
您可以执行以下操作:
$('.Gallery div a').click(function(e){
var index = $(this).index(); //this is the index of your currently clicked element
//do your rest of logic here
});
以上操作会将点击事件绑定到所有a
个标记,这些标记具有父div并且父级具有Gallery
类。