简体   繁体   English

在lightgallery.js滑块中定位img

[英]target img in lightgallery.js slider

Is there a way to target the img showed in lightgallery.js slider ? 有没有一种方法可以瞄准lightgallery.js滑块中显示的img

This JS below has 3 fails to get a simple console.log(); 下面的这个JS有3个无法获得简单的console.log(); :

var $lg = $('#mosaic');
$lg.lightGallery({thumbnail: false, addClass: 'imgGallery'});

$('.imgGallery img').click(function() {
     console.log('click'); // fail
});
$('img.lg-object.lg-image').click(function() {
     console.log('click'); // fail
});
$('div.lg-img-wrap img').click(function() {
     console.log('click'); // fail
});

JSFIDDLE 的jsfiddle

The img elements that have the click event aren't inside the .imgGallery div. 带有click事件的img元素不在.imgGallery div中。 They're actually inside of #mosaic . 它们实际上位于#mosaic If you change that selector to $('#mosaic img') you're all set for that one. 如果将选择器更改为$('#mosaic img')那么您都已经准备好了。

As for the other two, those elements are created dynamically, so they don't exist at the time you're trying to bind the click events. 至于其他两个,这些元素是动态创建的,因此在您试图绑定click事件时它们不存在。 What you want is click delegation, so that you're binding the click event to an outer element, but intercepting it on a child. 您想要的是单击委托,以便将click事件绑定到外部元素,但在子元素上拦截它。 So create a wrapper around everything (or if not possible, bind to the body) 因此,围绕所有内容创建一个包装器(或者,如果不可能,请绑定到主体)

$('body').click('img.lg-object.lg-image', function(e) {
 console.log('click'); // success
 console.log(this); // body (element to which the event is bound)
 console.log(e.target); // img.lg-object.lg-image (element that received the click)
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM