Jquery provides a noconfilict API to resolve conflicts.
Usage:
Jquery. noconflict (): Run this function to assign control of variable $ to the first library to implement it.
Jquery. noconflict (true): returns control of $ and jquery to the original library.
First:
Jquery. noconflict ();//Do something with jqueryJquery ("Div P"). Hide ();//Do something with another library's $ ()$ ("Content"). style. Display = 'none ';
Second:
Jquery. noconflict ();(Function($) {$ (Function(){//More code using $ as alias to jquery}) ;}) (Jquery );//Other code using $ as an alias to the other library
Third:
Jquery. noconflict ()(Function(){//Code using jquery});//Other code using $ as an alias to the other library
Fourth:
VaRJ =Jquery. noconflict ();//Do something with jqueryJ ("Div P"). Hide ();//Do something with another library's $ ()$ ("Content"). style. Display = 'none ';
Category 5:
VaRDOM ={}; Dom. Query= Jquery. noconflict (True);//Do something with the new jqueryDom. Query ("Div P"). Hide ();//Do something with another library's $ ()$ ("Content"). style. Display = 'none';//Do something with another version of jqueryJquery ("div> P"). Hide ();
Noconfilict source code:
Noconflict:Function(Deep ){If(Window. $ =Jquery) {window. $=_ $ ;}If(Deep & window. jquery =Jquery) {window. jquery=_ Jquery ;}ReturnJquery ;},
It is defined at the beginning of jquery source code:
(Function(){VaR//_ Jquery =Window. jquery, _ $=Window. $,//})();
To prevent $ and from being overwritten by jquery, save.
When deep is empty, the regular function of "_ $" to overwrite "window. $" is invalid, but jquery can continue to use it. When "$" is redefined in a new library, "jquery" continues to be a common function of jqquery, and "$" is not in jquery, it is a common function of the new database;
When deep is not empty, it overwrites "_ jquery" to "window. jquery", which may invalidate the jquery plug-in. In addition, the jquery returned by the method is not actually overwritten. It can be used to move to a new namespace, suchDom. query = jquery. noconflict (true); Dom. Query ("Div P"). Hide ();