在变量中发送jquery回调函数

Bas*_*adi 9 jquery

我有这个jquery函数

    function example(file, targetwidget, callback){

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

我做的时候工作正常:

 example('http://example.com/', "#divid", callback);
Run Code Online (Sandbox Code Playgroud)

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

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

然后函数必须是这样的:

function example(file, targetwidget, callback){

$(targetwidget).load(file, {limit: 25}, function(){
    callback;
});
Run Code Online (Sandbox Code Playgroud)

但那不起作用

在此先感谢您的帮助

Dav*_*ard 19

要传递回调,变量必须是函数类型.任何这些应该工作:

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);
Run Code Online (Sandbox Code Playgroud)