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
在此先感谢您的帮助
5 个解决方案
#1
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);
#2
you need to invoke the function inside the callback
你需要在回调中调用函数
function example(file, targetwidget, callback){
$(targetwidget).load(file, {limit: 25}, function(){
callback();
});
#3
This should work:
这应该工作:
$(targetwidget).load(file, {limit: 25}, callback);
#4
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 this
keyword!
回调的参数将帮助您将数据发送回回调函数。注意,不要忘记这个关键字!
#5
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()方法调用函数。
#1
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);
#2
you need to invoke the function inside the callback
你需要在回调中调用函数
function example(file, targetwidget, callback){
$(targetwidget).load(file, {limit: 25}, function(){
callback();
});
#3
This should work:
这应该工作:
$(targetwidget).load(file, {limit: 25}, callback);
#4
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 this
keyword!
回调的参数将帮助您将数据发送回回调函数。注意,不要忘记这个关键字!
#5
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()方法调用函数。