NoConflict () method in jQuery

Source: Internet
Author: User

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! ");
});
});

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.