jQuery.noConflict()The function is used to give the jquery library control over the variable $ (and the variable jquery) .
In general, in the jquery library, variables $ are aliases of variables jQuery , which are equivalent, for example, jQuery("p") and $("p") are equivalent. Since the variable has $ only one character and is distinctive, we are more accustomed $ to using it to manipulate the jquery library.
However, other JS libraries may also use variables $ to operate, such as the prototype library. At this point two libraries may $ conflict due to variable control issues.
At this point, you can use this function to give the jquery library control over the variable, the variable to the $ previous implementation of its JS library , and then we can only use variables jQuery to manipulate the jquery library.
In addition, using this function, you can also give $ control of variables and variables at the same time jQuery , enabling multiple versions of the jquery library to coexist (see the example below for details).
The function belongs to the global jQuery object.
Grammar
jQuery.noConflict() The syntax for a static function is as follows:
JQuery. Noconflict([])
Parameters
| Parameters |
Description |
| RemoveAll |
The optional/boolean type is the complete handover of control over the variable jquery, by default false . |
If the argument is omitted removeAll or the argument is not, it means that only the control of the true variable is conceded $ ; true $ jQuery
return value
jQuery.noConflict()The return value of the function is the jquery type, which returns jQuery a reference to the variable.
Example & Description
1. The following are the loading of prototype and jquery libraries:
<script Type="Text/javascript" Src="Prototype.js"></script>
<script Type="Text/javascript" Src="Jquery.js"></script>
<script Type="Text/javascript">
//give control over variable $
Jquery.
//use JQuery for DOM operations
Jquery ( "# Uname "). hide ();
//use prototype for DOM operations
$ ( " Mydiv "). setstyle ( {color: "#ffffff" }
</SCRIPT>
2. We can also manipulate jquery by using other custom variable names:
Give up control over the variable $ and assign jquery to the new alias J
var J = Jquerynoconflict ();
//DOM operations based on jquery (using variable j)
J (hide ();
//based on prototype DOM operations
$ ( " Mydiv "). setstyle ( {color: "#ffffff" }
3. Even if multiple libraries coexist, we can use the jQuery.ready() local variable as an alias for jquery in the callback function or other custom function $ :
Give the jquery library control over the variable $
Jquery.Noconflict();
Jquery(Document).Ready(function($){
Use local variable $ for jquery operations
$("P").CSS("Color", " ");
});
(function($) {
//Use local variable $ for jquery operation
$("ul Li"). AddClass("item");
} (jQuery));
4, if you want to achieve two versions of the jquery library coexistence, we can write the following code:
<script Type="Text/javascript" Src="Jquery-1.4.2.js"></script>
<script Type="Text/javascript" Src="Jquery-1.11.1.js"></script>
<script Type="text/JavaScript">
Give jQuery-1.11.1 control over variable $ and variable jquery
VarJ=Jquery.Noconflict( True );
Document.Writeln(J.Fn.Jquery); 1.11.1
Document. Writeln ( $. Fn. jquery //1.4.2
Document. ( Jquery. Fn. jquery //1.4.2
/*
* If the preceding jquery.noconflict () does not have an incoming parameter of true,
* means that only the control of the variable $ is conceded, then $ means 1.4.2,jquery represents 1.11.1
* at this time, JQuery.fn.jquery is 1.11.1
*/
</SCRIPT>
5, three versions of the jquery library coexist, the corresponding jquery sample code is as follows:
<script Type="Text/javascript" Src="Jquery-1.4.2.js"></script>
<script Type="Text/javascript" Src="Jquery-1.8.3.js"></script>
<script Type="Text/javascript" Src="Jquery-1.11.1.js"></script>
<script Type="text/JavaScript">
Let jQuery-1.11.1 control the variable $ and the variable jquery, using the variable J to control
VarJ=Jquery.Noconflict( True );
Give jQuery-1.8.3 control over the variable $
Jquery.Noconflict();
Document.Writeln( J.. jquery //1.11.1
Document. ( Jquery. Fn. jquery //1.8.3
Document. ( $. Fn. jquery //1.4.2
</SCRIPT>
Note : Multiple JS libraries may have conflicting names of global variables, and the actual control of variables usually depends on the loading order of JS libraries. In the example code for the three previous versions of the jquery library, the variable that was loaded in the jquery library overwrites the previous version of the variable, so the control is handed over to the previous JS library each time the variable is given control.
Jquery.noconflict () function