JQuery and other JavaScript frameworks
As you have learned, jQuery uses the $ symbol as short for jQuery.
What if other JavaScript frameworks use the $ symbol for short?
Other JavaScript frameworks include: MooTools, Backbone, Sammy, Cappuccino, Knockout, JavaScript MVC, Google Web Toolkit, Google Closure, Ember, Batman, and Ext JS.
Some frameworks also use the $ symbol as short form (like jQuery). If the two frameworks you are using use the same short form symbol, the script may stop running.
The jQuery team considered this issue and implemented the noConflict () method.
JQuery noConflict () method
The noConflict () method releases the control of $ identifier, so that other scripts can use it.
Instance
Of course, you can still use jQuery by replacing the abbreviation with the full name:
Copy codeThe Code is as follows:
$. NoConflict ();
JQuery (document). ready (function (){
JQuery ("button"). click (function (){
JQuery ("p"). text ("jQuery is still running! ");
});
});
Instance
You can also create your own shorthand. NoConflict () returns a reference to jQuery. You can save it to a variable for later use. See this example:
Copy codeThe Code is as follows:
Var jq = $. noConflict ();
Jq (document). ready (function (){
Jq ("button"). click (function (){
Jq ("p"). text ("jQuery is still running! ");
});
});
Instance
If your jQuery code block uses the $ Abbreviation and you do not want to change this shortcut, you can pass the $ symbol as a variable to the ready method. In this way, you can use the $ symbol in the function. In addition to the function, you still have to use "jQuery ":
Copy codeThe Code is as follows:
$. NoConflict ();
JQuery (document). ready (function ($ ){
$ ("Button"). click (function (){
$ ("P"). text ("jQuery is still running! ");
});
});