javascript 使用 .trigger('click') 自动加载fancybox

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4439576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 11:26:26  来源:igfitidea点击:

Auto load fancybox using .trigger('click')

javascripttriggersfancyboxautoload

提问by Hung Bui

I was just wondering if you could please help. I am trying to get the fancybox to load automatically once the page is rendered. However, I got this error message 't is undefined' at line 18 (/js/fancybox/ jquery.fancybox-1.3.3.pack.js). At the moment, I am using jquery version 1.4.2

我只是想知道你是否可以帮忙。我试图在页面呈现后自动加载fancybox。但是,我在第 18 行 (/js/fancybox/ jquery.fancybox-1.3.3.pack.js) 收到此错误消息“t is undefined”。目前,我使用的是 jquery 1.4.2 版

<a href="#container" id="profile">Click me</a>

<div style="display:none">
<div id="container">Fancybox Content Here .... </div>
</div>

<script type="text/javascript">
$(document).ready(function(){
      $('#profile').fancybox().trigger('click');

});

</script>

The funny thing is if I take the line below and run it in firebug console then it WORKS!. Not sure what did I do wrong here. Your helps would be grateful.

有趣的是,如果我采用下面的行并在萤火虫控制台中运行它,那么它就可以工作了!。不知道我在这里做错了什么。您的帮助将不胜感激。

 $('#profile').fancybox().trigger('click');

Thanks

谢谢

回答by Krevindiou

I got the same problem. Testing with the unpacked version, i got "loading is undefined" in line 36 (fancybox 1.3.4). So I called "$.fancybox.init();" before triggering and it works correctly now.

我遇到了同样的问题。使用解压版本进行测试,我在第 36 行(fancybox 1.3.4)中得到“加载未定义”。所以我叫“$.fancybox.init();” 在触发之前,它现在可以正常工作。

回答by Val

Nope th reason is because the fancybox.jsis included twice this is the file that has the core of fancy box

不是原因是因为fancybox.js包含了两次这是具有花式盒子核心的文件

回答by Sergey Narozhnyy

I've managed with the similar case in the way like this:

我以这样的方式处理了类似的案例:

$(".some_wrapper").on('click', function(){
    var lnk = $(this).find("a").first();
    if (hr && ~cls.indexOf("fancybox-a")) {
        $.fancybox(lnk);
    }
});