Jquery-fancybox 和回调

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/910455/
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-08-26 10:06:26  来源:igfitidea点击:

Jquery - fancybox and callback

jqueryfancybox

提问by sasa

I have problem with fancybox.
I want to write a function that will run when the fancybox opened. How and when to call the function?

我对fancybox 有问题。
我想编写一个在fancybox打开时运行的函数。如何以及何时调用该函数?

Example:

例子:

function myFunc() {
    alert("Opened!");
}

$('.content a').fancybox({ 
    'hideOnContentClick': false ,
    'callBackOnShow': myFunc(), // This not working! It call function when page is loaded
    'frameWidth': 920,
    'frameHeight': 530
});

回答by Matt Briggs

Instead of myFunc()use myFunc. In javascript, a function with parens (and/or args) means you want to call it. A function name without them means you just want a reference to that function (which is probably what fancybox wants from you)

而不是myFunc()使用myFunc. 在 javascript 中,带有括号(和/或 args)的函数意味着您要调用它。没有它们的函数名意味着您只需要对该函数的引用(这可能是fancybox 想要的)

回答by Gabriel

This works

这有效

$("#element").fancybox({
    onStart     :   function() {
        return window.confirm('Continue?');
    },
    onCancel    :   function() {
        alert('Canceled!');
    },
    onComplete  :   function() {
        alert('Completed!');
    },
    onCleanup   :   function() {
        return window.confirm('Close?');
    },
    onClosed    :   function() {
        alert('Closed!');
    }
});

回答by Qwerty

The newest way how to make it work is using afterShow or beforeShow:

使其工作的最新方法是使用 afterShow 或 beforeShow:

$("a#registrace").fancybox({
  beforeShow   : function() {
    alert('its working!');
    validate_reg_form();
  }
});

I am loading registration form through ajax and I need to enable form-validation. In both cases (before and after), the content is already loaded.

我正在通过 ajax 加载注册表单,我需要启用表单验证。在这两种情况下(之前和之后),内容已经加载。

回答by Rik Heywood

Actually, on checking the docs, you have a typo in your option list...

实际上,在检查文档时,您的选项列表中有一个错字...

You have callBackOnShowbut it should be callbackOnShow(lower case b)

你有,callBackOnShow但它应该是callbackOnShow(小写 b)

回答by Mario Menger

'callbackOnShow': myFunc

lower case b as the options are case sensitive, and no brackets on myFunc, as you want to pass the function as an option, not call it.

小写 b 因为选项区分大小写,并且 myFunc 上没有括号,因为您想将该函数作为选项传递,而不是调用它。

回答by Rik Heywood

It should be:-

它应该是:-

function myFunc() {
    alert("Opened!");
}

$('.content a').fancybox({ 
    'hideOnContentClick': false ,
    'callBackOnShow': myFunc, // no brackets
    'frameWidth': 920,
    'frameHeight': 530
});

Or, you can make an anonymous function...

或者,您可以创建一个匿名函数...

$('.content a').fancybox({ 
    'hideOnContentClick': false ,
    'callBackOnShow': function() { alert('hello'); },
    'frameWidth': 920,
    'frameHeight': 530
});

回答by James Nicholson

As your title says "Jquery - fancybox and callback", You can a complete list of callback options for the fancybox 2 plugin found under the tab callbacks.

正如您的标题所说的“Jquery-fancybox 和回调”,您可以在选项卡回调下找到fancybox 2 插件的回调选项的完整列表。

http://fancyapps.com/fancybox/#docs

http://fancyapps.com/fancybox/#docs

If your using the old fancybox, maybe upgrading to fancybox2 as the new plugin has better features, with more control.

如果您使用旧的fancybox,可能会升级到fancybox2,因为新插件具有更好的功能和更多的控制。