如何编写一个接受回调作为参数的jquery函数 [英] How do I write a jquery function that accepts a callback as a parameter
本文介绍了如何编写一个接受回调作为参数的jquery函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下功能。
function ChangeDasPanel(controllerPath, postParams) {
$.post(controllerPath, postParams, function(returnValue) {
$('#DasSpace').hide("slide", { direction: "right" }, 1000, function() {
$('#DasSpace').contents().remove();
$('#DasSpace').append(returnValue).css("display", "block");
$('#DasSpace').show("slide", { direction: "right" }, 1000);
});
});
};
但我想能像这样调用
ChangeDasPanel("../Home/Test", {} ,function (){
//do some stuff on callback
}
如何在我的函数中实现对回调的支持?
How can I implement support for callbacks in my function?
推荐答案
function ChangeDasPanel(controllerPath, postParams, f) {
$.get(
controllerPath,
postParams,
function(returnValue) {
var $DasSpace = $('#DasSpace');
$DasSpace.hide(
"slide", { direction: "right" }, 1000,
function() {
$DasSpace.contents().remove();
$DasSpace.append(returnValue).css("display", "block");
$DasSpace.show("slide", { direction: "right" }, 1000);
}
);
if (typeof f == "function") f(); else alert('meh');
}
);
};
您可以像JavaScript中的任何其他对象一样传递函数。传递一个回调函数是直接的,你甚至自己在 $。post()
调用。
You can pass functions like any other object in JavaScript. Passing in a callback function is straight-forward, you even do it yourself in the $.post()
call.
您可以决定是否要将回调作为 $。post()
回调的一部分调用或自行调用。
You can decide whether you want to have your callback called as part of the $.post()
callback or on its own.
这篇关于如何编写一个接受回调作为参数的jquery函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文