gpt4 book ai didi

jQuery 覆盖 $.post 函数

转载 作者:行者123 更新时间:2023-12-01 00:24:09 26 4
gpt4 key购买 nike

首先,我为我糟糕的英语道歉......希望有人能理解我的问题并帮助我......

我正在开发一个使用大量 $.post 调用的项目,我想通过为所有调用添加相同的验证来改进它们。

我不想一一更改所有脚本,那么有没有办法覆盖 $.post 函数以同时向所有脚本添加相同的内容?或者也许是在每个 $.post 上触发另一个功能的方法?像这样的东西:

$.post.each(function(){[...]});

$('body').on('post', function(){[...]});

谢谢!

编辑:终于如我所愿了!这是我的代码:

// Override $.post
(function(){
// Store a reference to the original remove method.
var originalPostMethod = jQuery.post;

// Define overriding method.
jQuery.post = function(data){
// Execute the original method.
var callMethod = originalPostMethod.apply( this, arguments);

callMethod.success(function(){
//console.log('success');
});
callMethod.error(function(){
//console.log('error');
});
callMethod.complete(function(){
//console.log('complete');
return callMethod;
});
}
})();

最佳答案

$.post = function() {
var _method = $.post;
// do something you want
console.log("do someting");
return _method.apply($, Array.prototype.slice.apply(arguments));
}

关于jQuery 覆盖 $.post 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11930295/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com