jQuery覆盖$ .post函数 [英] jQuery override $.post function
问题描述
我正在使用一个项目很多$ .post调用,我想通过添加相同的验证来改进它们。
我不想逐个更改所有脚本,所以有没有什么办法可以重写$ .post函数来同时向它们添加相同的东西?
或者也许可以在每个$ .post上触发另一个函数?
类似于:
$。post.each(function(){[...]});
或
<$ c'c> $('body')。on('post',function(){[...]});
谢谢!
编辑:
最后做到了,因为我想!
这里是我的代码:
$ $ p $ $ $ c $ //覆盖$ .post
(function(){
$ //存储对原始移除方法的引用
var originalPostMethod = jQuery.post;
//定义重写方法
jQuery.post = function(data) {
//执行原始方法
var callMethod = originalPostMethod.apply(this,arguments);
$ b $ 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;
//做你想要的东西
console.log(do someting);
return _method.apply($,Array.prototype.slice.apply(arguments));
}
First of all I apologize for my poor english... Hope someone will understand my question and help me...
I'm working on a project using lots of $.post call and I want to improve them by adding the same verification for all of them.
I do not want to change all scripts one by one so is there any way to override the $.post function to add the same thing to all of them at the same time ? Or maybe a way to trigger another function on each $.post ? Something like :
$.post.each(function(){[...]});
or
$('body').on('post', function(){[...]});
Thanks !
EDIT : Finally did it as I wanted ! Here is my code :
// 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函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!