在 jQuery 中传递函数作为参数?
我想向 jQuery 函数传递一个常规函数,而不是通常的匿名函数,但我不确定如何完成这样的事情。
而不是这样:
function setVersion(feature) {
$.post("some.php", { abc:"abc" },
function(data){
// do something here
}, "json");
}
我想这样做:
function foo(data){
// do something here
}
function setVersion(feature) {
$.post("some.php", { abc:"abc" }, foo, "json");
}
谢谢。
I would like to pass to a jQuery function a regular function, instead of the usual anonymous function, but I'm not sure how such a thing could be done.
Instead of this:
function setVersion(feature) {
$.post("some.php", { abc:"abc" },
function(data){
// do something here
}, "json");
}
I would like to do this:
function foo(data){
// do something here
}
function setVersion(feature) {
$.post("some.php", { abc:"abc" }, foo, "json");
}
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,已经可以使用了。但你希望它可能看起来像这样:
Yeah, already works. But you want it probably look like this:
应该运行得很好。
我相信 jQuery 实际上是要使用按名称调用的常规函数。使用匿名函数只是替换本来会被传递的命名函数。
Should run just fine.
I believe jQuery is actually meant to use the regular function, called by name. Using the anonymous function is simply a replacement for a named function that would otherwise be passed.
是的,你就是这么做的。
Yes, that is exactly how you do it.