7

我想向 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");
}

谢谢你。

4

3 回答 3

12

是的,已经可以了。但你希望它可能看起来像这样:

function setVersion(feature, myFunction) {
      $.post("some.php", { abc:"abc" }, myFunction, "json");
}
setVersion(blah, foo);
于 2010-04-17T20:59:10.453 回答
2

应该运行得很好。

我相信 jQuery 实际上是为了使用按名称调用的常规函数​​。使用匿名函数只是一个命名函数的替代品,否则会被传递。

于 2010-04-17T20:59:52.993 回答
1

是的,这正是你的做法。

于 2010-04-17T20:56:54.410 回答