A detailed resolution of jquery's $ naming conflict

Source: Internet
Author: User

  in jquery, $ is the alias of jquery, and all places using $ can be replaced with jquery, such as $ (' #msg ') equivalent to jquery (' #msg '). However, when we introduce a number of JS libraries, in another JS library also defines the $ symbol, then we use the $ symbol when the conflict occurred

In jquery, $ is the alias of jquery, and all uses of $ can be replaced with jquery, such as $ (' #msg ') equivalent to jquery (' #msg '). However, when we introduce a number of JS libraries, in another JS library also defines the $ symbol, then we use the $ symbol when there is a conflict. The following is an example of introducing two library files Jquery.js and prototype.js.     The first case: Jquery.js prototype.js After the introduction, such as: <script src= "Prototype.js" type= "Text/javascript"/>  <script src= "Jquery.js" type= "Text/javascript"/>    In this case, we write in our own JS code as follows:   $ (' #msg '). Hide () ;    $ always represents the $ symbol defined in jquery, or it can be written as jquery (' #msg '). Hide (); If you want to use the $ defined in Prototype.js, we'll explain it later.     The second case: Jquery.js is introduced before prototype.js, such as:  <script src= "jquery.js" type= "Text/javascript"/>   <script src= "prototype.js" type= "Text/javascript"/>    In this case, we write in our own JS code as follows:   $ (' # Msg '). Hide ();    $ The $ symbol defined at this point in the prototype.js, if we want to invoke the factory selection function in Jquery.js, we can only use jquery (' #msg ') with the full name. Hide ().   below first introduced in the first type of JS library file order, how to correctly use the different JS library defined in the $ symbol.   I. Using the JQUERY.NOCONFLICT ()   The purpose of this method is to allow jquery to waive ownership of $ and return control of $ to prototype.js, since Jquery.js is introduced later, so the final owner of the $ controlIt's jquery. Its return value is jquery. When this method is called in code, we cannot use $ to invoke the JQuery method, at which point $ represents the $ defined in the Prototype.js library. As follows:   Jquery.noconflict ();  //This can no longer be written as $ (' #msg '). Hide (), at which point $ represents the $ symbol defined in Prototype.js.   Jquey (' #msg '). Hide ();    Since then $ is no longer available on behalf of the $,jquery.js defined in Prototype.js, only use the full name jquery in Jquery.js.     two. customizing jquery alias   If you think that the Jquery.noconflict () method is used in the first method, you can only use the jquery full name as a hassle, we could also redefine the alias for jquery. as follows:   var $j =jquery.noconflict ();  $j (' #msg '). Hide ()//Here $j on behalf of jquery  since then $ on behalf of the $ defined in Prototype.js, The $ in Jquey.js is no longer available and can only be used as an alias for jquery in Jquey.js using $j.     three. Using statement blocks, the $ as defined in Jquery.js is still used in the statement block, as follows: Jquery.noconflict ();  JQuery (document). Ready (function ($) {   $ (' #msg '). Hide ()//The $ that is used in the method of the entire ready event is jquery.js $. });    or use the following statement block:   (function ($ ) {  .....  $ (' #msg '). Hide ()//is used in this statement block as defined in Jquery.js $. }) (JQuery)     If in the second kind of Introduction JS library file order, how to use the $ in jquery.js, we can still use the method of the statement block described above, such as:   code as follows: <script src= "Jquery.js" type= "Text/jav"Ascript "/>  <script src=" prototype.js "type=" Text/javascript "/>  <script type=" text/ JavaScript ">    (function ($) {  .....  $ (' #msg '). Hide ()//is used in this statement block for the $ defined in Jquery.js.  }) (JQuery)   </script>     This method of using statement blocks is useful, and should be used when we write our own JQuery plug-ins. Because we do not know the specific work process is how to introduce a variety of JS library, and this sentence block writing can shield conflict.   (function ($) {}) (JQuery)   1 First (function () {}) This is done by creating an anonymous method and executing it immediately (function () {}) This is the parentheses behind the anonymous method that calls this method immediately. Doing so can create a scope to ensure that internal variables do not conflict with external variables, such as $ jquery, such as the internal definition of jquery variables.   2 (function ($) {}) (jquery) The main role is to ensure that jquery does not conflict with other libraries or variables first is to ensure that jquery this variable name and external no conflict (jquery internal $ and jquery is the same thing The reason for having two names is that they are afraid of having a conflict with other variable names two jquery has a very small chance of conflict with other variables and passes in anonymous objects, the anonymous object names the parameters as $ (actually the same as inside jquery) and then you are free to (function ($) {}) ( JQuery) to write your plugin without considering conflicts with external variables    

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.