在变量内发送 jquery 回调函数

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

send a jquery callback function inside a variable

jquery

提问by Bassel Safadi

I have this jquery function

我有这个 jquery 功能

    function example(file, targetwidget, callback){

    $(targetwidget).load(file, {limit: 25}, function(){
        $("#widget_accordion").accordion({fillSpace: true});
    });
}

it's working ok when I do:

当我这样做时它工作正常:

 example('http://example.com/', "#divid", callback);

but I want to send the callback function inside the (callback) variable i.e: instead of butting the $("#widget_accordion").accordion({fillSpace: true}); inside the callback function I want to send it:

但我想在(回调)变量中发送回调函数,即:而不是对接 $("#widget_accordion").accordion({fillSpace: true}); 在我想发送的回调函数中:

 example('http://example.com/', "#divid", '$("#widget_accordion").accordion({fillSpace: true});');

and then the function must be something like:

然后函数必须是这样的:

function example(file, targetwidget, callback){

$(targetwidget).load(file, {limit: 25}, function(){
    callback;
});

but that's not working

但这不起作用

Thanks in advance for your help

在此先感谢您的帮助

回答by Dave Ward

To pass the callback around, the variable needs to be of type function. Any of these should work:

要传递回调,变量需要是函数类型。其中任何一个都应该有效:

function example(file, targetwidget, callback) {
  $(targetwidget).load(file, {limit:25}, callback);
}

// Call it by providing the function parameter via inline anonymous function:
example('http://example.com/', "#divid", function() {
  $("#widget_accordion").accordion({fillSpace: true});
});

// Or, declare a function variable and pass that in:
var widgetCallback = function() {
  $("#widget_accordion").accordion({fillSpace: true});
};

example('http://example.com/', "#divid", widgetCallback);

// Or, declare the function normally and pass that in:
function widgetCallback() {
  $("#widget_accordion").accordion({fillSpace: true});
}

example('http://example.com/', "#divid", widgetCallback);

回答by redsquare

you need to invoke the function inside the callback

您需要调用回调中的函数

function example(file, targetwidget, callback){

$(targetwidget).load(file, {limit: 25}, function(){
    callback();
});

回答by peirix

This should work:

这应该有效:

$(targetwidget).load(file, {limit: 25}, callback);

回答by Ionu? Staicu

well.. You do this way:

好吧..你这样做:

function example(file, targetwidget, callback){

$(targetwidget).load(file, {limit: 25}, function(){
    if(typeof(callback)==='function'){
     callback.call(this, 'other parameters');
    }
});

The paramenters of callback will help you to send data back to callback function. ATTENTION, don't forget the thiskeyword!

回调参数将帮助您将数据发送回回调函数。注意,不要忘记this关键字!

回答by googletorp

Since you are passing a text string you would need to eval it:

由于您正在传递文本字符串,因此您需要对其进行评估:

function example(file, targetwidget, callback){

    $(targetwidget).load(file, {limit: 25}, function(){
        eval(callback);
    });

Edit:

编辑:

if you want to use call() you would have to do it like this:

如果你想使用 call() 你必须这样做:

function callback() {
    $("#widget_accordion").accordion({fillSpace: true});
}

function example(file, targetwidget, callback){

    $(targetwidget).load(file, {limit: 25}, function(){
        if(typeof(callback)==='function'){
            callback.call(this);
        }
    });

example('http://example.com/', "#divid", callback);

In order for call to work, you would have to pass a function and not a string. But by wrapping your jQuery expression inside of a function, it would be run when the function is called. Calling a function can be done with the .call() method.

为了使调用起作用,您必须传递一个函数而不是一个字符串。但是通过将您的 jQuery 表达式包装在一个函数中,它会在函数被调用时运行。可以使用 .call() 方法调用函数。