简体   繁体   English

如何在.on(“ click”)函数中获取jquery上的第一个元素而不是其child()?

[英]How to get the first element and not its children() on jquery in a .on(“click”) function?

I have this gallery slider plugin called LightSlider that I just got a few days in the internet, and I've added some legend bars floating in the bottom of the image, so I was trying to make the fullscreen click button to be the image, and not the legend bars I've made. 我有一个名为LightSlider的图库滑块插件,我刚刚在互联网上待了几天,并且在图像底部添加了一些图例栏,所以我试图使全屏单击按钮成为图像,而不是我制作的图例栏。 When I click in the image nothing happens, but when I click in the legend bars I've added the gallery plugin shows up in full screen. 当我单击图像时,什么都没有发生,但是当我单击图例栏时,我添加的图库插件将全屏显示。

So I noticed that it's happening thanks to the $(this).children() that I found on the responsible line for the fullscreen call. 所以我注意到,由于我在负责全屏调用的负责行上找到了$(this).children() ,所以这种情况正在发生。

Here is the relevant part of the script: 这是脚本的相关部分:

$children.on('click', function (e) {
    if (settings.selector !== null) {
        $children = $(settings.selector);
    } else {
        $children = $this.children();
    }
    e.preventDefault();
    e.stopPropagation();
    index = $children.index(this);
    prevIndex = index;
    setUp.init(index);
});

The thing is that when I changed the first line $children.on('click') to $('img:first-child').on('click') it worked to make the image call the fullscreen and not the legend bars, but then everytime I go on fullscreen the initial image is the same, and not the one that I clicked. 关键是,当我将第一行$children.on('click')更改$('img:first-child').on('click')它可以使图像全屏显示,而不是图例条,但是每次我全屏显示时,初始图像都是相同的,而不是单击的图像。

Obs.: The images in the gallery are all listed by a loop on php 观察:画廊中的图像全部通过php循环列出

The $children.index(this) is what I believe to be the responsible for the initial image on fullscreen... $children.index(this)是我认为负责全屏显示初始图像的原因...

Is there anything I should put to replace this code to make it work? 我有什么可替换的代码以使其起作用?

$(document).on('click',function(e){
    if($(this).closest($children).length > 0)
        e.stopPropagation();
})

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

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