This plug-in is used to add a callback function to the jQuery method. You can add any custom callback function to the class method or instance method without affecting the behavior of the original method. For more information, see, I hope this article will help you with the plug-in source code jquery. callback. js
Plug-in Open Source Address: https://gist.github.com/4580276
The Code is as follows:
/**
* @ FileOverview this plug-in is used to add a callback function to the jQuery method. You can add any custom callback function to the class method or instance method without affecting the behavior of the original method.
* @ Dependency jQuery1.7 +
* @ Author huhai
* @ Since 2013-01-21
*/
(Function ($ ){
$. _ Callbacks = {};
$. _ Callbacks _ = {};
$. _ Alias = {};
$. _ Alias _ = {};
$. Extend ({
/**
* @ Decription Add a callback function to the Method
* @ Param funcName: string name of the callback function to be added
* @ Param callback: function callback function (if you want to remove it, do not use the anonymous method)
* @ Param static: whether the boolean is a class method. The default value is false.
*/
AddCallback: function (funcName, callback, static ){
If ("string" === typeof (funcName) & $. isFunction (callback )){
If (static = true ){
If ($ [funcName] & $. isFunction ($ [funcName]) {
If (! This. _ callbacks [funcName]) {
This. _ callbacks [funcName] = $. Callbacks ();
}
This. _ callbacks [funcName]. add (callback );
If (! $. _ Alias [funcName]) {
$. _ Alias [funcName] = $ [funcName]; // store the original Class Method
$ [FuncName] = function () {// proxy class method;
Var result = $. _ alias [funcName]. apply (this, arguments );
$. _ Callbacks [funcName]. fireWith (this, arguments );
Return result;
};
}
}
} Else {
If ($. fn [funcName] & $. isFunction ($. fn [funcName]) {
If (! This. _ callbacks _ [funcName]) {
This. _ callbacks _ [funcName] = $. Callbacks ();
}
This. _ callbacks _ [funcName]. add (callback );
If (! $. _ Alias _ [funcName]) {
$. _ Alias _ [funcName] = $. fn [funcName]; // Method for storing the original Instance
$. Fn [funcName] = function () {// proxy instance method;
Var result = $. _ alias _ [funcName]. apply (this, arguments );
$. _ Callbacks _ [funcName]. fireWith (this, arguments );
Return result;
};
}
}
}
}
},
/**
* @ Decription remove the callback function added to the Method
* @ Param funcName: name of the callback function added to the string
* @ Param callback: function callback function
* @ Param static: whether the boolean is a class method. The default value is false.
*/
RemoveCallback: function (funcName, callback, static ){
If ("string" === typeof (funcName) & $. isFunction (callback )){
If (static = true ){
If ($ [funcName] & $. isFunction ($ [funcName]) {
If (this. _ callbacks [funcName]) {
This. _ callbacks. remove (callback );
}
}
} Else {
If ($. fn [funcName] & $. isFunction ($. fn [funcName]) {
If (this. _ callbacks _ [funcName]) {
This. _ callbacks _. remove (callback );
}
}
}
}
}
});
}) (JQuery );
Usage example:
HTML
The Code is as follows: