JQuery Core-Noconflict () method

Source: Internet
Author: User

One. Use the Noconflict () method to specify a new name for the jQuery variable: var jq=$.noconflict ();

1 var jq=$.noconflict (); 2 JQ (document). Ready (function() {3   jq ("button"). Click (function () {4     jq ("P"). Hide (); 5   }); 6 });

Definition and usage

The 1.noConflict () method gives the JQuery control over the variable $.

2. This method releases JQuery's control over the $ variable.

3. This method can also be used to specify a new custom name for the jQuery variable.

Tip : This method is useful when other JavaScript libraries use $ for their functions.

Note: The simple way to say $.noconflict () is to release the control of the $ variable in jQuery;

Two. Many JavaScript libraries use $ as functions or variable names, as does jQuery. In jquery, $ is just an alias for jquery, so all functionality is guaranteed even if you do not use $. If we need to use another JavaScript library other than JQuery, we can return control to the library by calling $.noconflict ():

<script type= "Text/javascript" >  $.noconflict ();   // code that uses another library of $ </script>

Can be used in conjunction with the. Ready () method to alias the JQuery object, which is very effective:

<script type= "Text/javascript" src= "other_lib.js" ></script><script type= "Text/javascript" src= " Jquery.js "></script><script type=" Text/javascript ">  $.noconflict ();  jquery (document). Ready (function($) {    //  code using jQuery $   });   //  use a $ code for other libraries </script>

Example
1. Map the referenced object back to the original object:

jquery.noconflict (); JQuery("div p"). Hide ();    // use JQuery$ ("content"). Style.display = "None";    // use $ () for other libraries

2. Restore the alias $, then create and execute a function that will still be used as the alias of JQuery in the scope of the function. In this function, the original $ object is invalid. This function is useful for most plug-ins that do not depend on other libraries:

jquery.noconflict ();( function ($) {   $ (function() {    ///  use $ as JQuery alias code    // code for other libraries with $ as aliases

3. You can combine jquery.noconflict () with the shorthand ready to make your code more compact:

Jquery.noconflict () (function() {    / / code  using jQuery //  Other libraries use the code that makes aliases

4. Create a new alias to use the JQuery object in the next library:

var j = jquery.noconflict (); J ("Div p"). Hide ();    // JQuery-based code $ ("content"). Style.display = "None";    // $ () code based on other libraries

5. Completely move jQuery to a new namespace:

var dom == jquery.noconflict (true);

Results:

Dom.query ("div p"). Hide ();    // code for the new JQuery $ ("content"). Style.display = "None";    // code for another Library $ () jQuery ("div > P"). Hide ();    // Another version of JQuery code

JQuery Core-Noconflict () method

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.