Jquery扩展现有功能

时间:2012-01-10 05:18:01

标签: jquery extend

我正在寻找一种方法来扩展现有的Jquery函数,为它提供更多选项/参数。

我想使用的是$ .ajax,但这可以应用于任何jquery函数。

我希望能够调用这样的函数:

$.adv_ajax()

这将是$ .ajax函数的扩展版本,如下所示:

 $.adv_ajax({
  custom_parameter: "abc", //my custom one
  url: "test.html",
  context: document.body,
  success: function(){
    $(this).addClass("done");
  }
});

2 个答案:

答案 0 :(得分:8)

这样的事情:


(function($)
{
    // maintain a to the existing function
    var oldAjax = $.ajax;
    // ...before overwriting the jQuery extension point
    $.fn.ajax = function()
    {
        // original behavior - use function.apply to preserve context
        var ret = oldAjax.apply(this, arguments);


        // preserve return value (probably the jQuery object...)
        return ret;
    };
})(jQuery);


答案 1 :(得分:3)

那么只需将您的函数附加到jQuery对象:

$.adv_ajax = function(options) {
    // Do stuff like change some options and then call the original
    return $.ajax(options);
}
相关问题