Jquery扩展现有function
我正在寻找一种方法来扩展现有的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"); } });
那么只需将您的函数附加到jQuery对象:
$.adv_ajax = function(options) { // Do stuff like change some options and then call the original return $.ajax(options); }
像这样的东西:
(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);
(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);