在变量内发送一个jQuery回调函数 [英] send a jquery callback function inside a variable

查看:70
本文介绍了在变量内发送一个jQuery回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个jQuery函数

I have this jquery function

    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);

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

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:

 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;
});

但这不起作用

预先感谢您的帮助

推荐答案

要传递回调,变量必须为function类型.其中任何一个都可以工作:

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);

这篇关于在变量内发送一个jQuery回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆