且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何编写一个接受回调作为参数的jquery函数

更新时间:2022-06-04 06:50:21

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.