Javascript Jquery gallery/lightbox onClick始终返回-1“;

Javascript Jquery gallery/lightbox onClick始终返回-1“;,javascript,jquery,html,Javascript,Jquery,Html,我有一个包含12个链接(图像)的图库,当单击打开lightbox并显示单击的图像时。但是当我点击图像时,它总是打开最后一个。当我试图控制日志索引时,我总是得到“-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 h

我有一个包含12个链接(图像)的图库,当单击打开lightbox并显示单击的图像时。但是当我点击图像时,它总是打开最后一个。当我试图控制日志索引时,我总是得到“-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
类。

您只发布了一半的代码。你参加的是什么项目?什么元素是
e.target
$(e.target).index()返回什么?请发布您的完整代码。什么是e.target?这是什么事件?任何图像点击都返回“0”@ounnowno?
$('.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
});