简体   繁体   English

为什么我的fancybox 没有在$('.selector').click() 上打开?

[英]Why does my fancybox not open on $('.selector').click()?

I have a page of styled links that I broke into sections using jQuery.我有一页样式链接,我使用 jQuery 将其分成几个部分。

  1. Using jQuery .internal causes the page to navigate to the specified target specified by the href attribute of the link inside the div the user clicked on.使用 jQuery .internal会使页面导航到用户单击的 div 内链接的href属性指定的指定目标。
  2. .external does the same thing as .internal except it opens in a new tab. .external.internal执行相同的操作,只是它在新选项卡中打开。
  3. .video should simply cause the div clicked to play the video specified by the link in a fancybox but it does not. .video应该只是导致单击的 div 播放由fancybox 中的链接指定的视频,但事实并非如此。 Nor does it report an error in the console.它也不会在控制台中报告错误。

Here is my code for the fancybox:这是我的fancybox代码:

HTML HTML

<div id="fentanylVid" class="col-sm-3 dept video" data-department="fentanyl the real deal">
    <div class="box listed-left animated-content move_right animate clearfix">
        <div class="box-text">
            <h4><a data-fancybox="" href="https://youtu.be/Tt0dFCuwkfQ?rel=0">Fentanyl: The Real Deal (Video)</a></h4>
        </div>
    </div>
</div>    

jQuery jQuery

$('.video').click(function(){
    $().fancybox({
        selector : '.video'
    });
});

I also have the two resources in the header of my page我的页面的 header 中也有两个资源

You can either initialize the fancybox like this你可以像这样初始化fancybox

$('.video').fancybox({
    selector : '.video'
});

or as @Taplar said或如@Taplar 所说

$('.video').click(function(){
    $.fancybox.open(this)
});

A bit explaining what your code does:稍微解释一下您的代码的作用:

$('.video').click(function(){ // <- Here you are attaching your click event on selected items
    // So, when user clicks, this happens:
    $().fancybox({ // Here you are telling fancybox to attach click event ..
        selector : '.video'  // .. on this selector
    });
});

So, basically you have done too much work and all you have to do is to remove your own click event and it should work fine.所以,基本上你已经做了太多的工作,你所要做的就是删除你自己的点击事件,它应该可以正常工作。 Or you can use API to start fancybox programmatically, like in the other answer.或者您可以使用 API 以编程方式启动fancybox,就像在另一个答案中一样。

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

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