Solve the problem of the dollar symbol naming conflict in jquery

Source: Internet
Author: User

  in jquery, $ is the alias for jquery, and all uses of $ can be replaced with jquery, and here's how to solve the dollar sign naming conflict problem in jquery

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 is introduced after prototype.js, such as: <</span> script src = "prototype.js" type = "Text/javascri PT "/>  <</span> script src =" jquery.js "type =" Text/javascript "/>  in this case, we write in our own JS code as follows Words:  $ (' #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 introduce you later.     The second case: Jquery.js is introduced before prototype.js, such as: <</span> script src = "jquery.js" type = "Text/javascript "/>  <</span> script src =" prototype.js "type =" Text/javascript "/>  in this case, we write in our own JS code as follows Words:  $ (' #msg '). Hide ();  $ for the $ symbol defined in Prototype.js, if we want to invoke the factory selection function in Jquery.js, we can only use jquery (' #msg ') in full name. Hide () .    below, first introduce the order of the first JS library file, how to correctly use the different JS library defined in the $ symbol.     I. Use of JQUERY.NOCONFLICT () &nbsP The purpose of this method is to allow jquery to relinquish ownership of $ and return control of $ to prototype.js, since Jquery.js is introduced later, so the last possession of $ control is 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 (); //here 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. The following:  var $j = jquery.noconflict ();  $j (' #msg '). Hide (); Here $j on behalf of jquery  since then $ is no longer available on behalf of the $,jquey.js defined in Prototype.js, only $j can be used as the alias for jquery in Jquey.js.     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 $ used in the method of the entire ready event is the $.} defined in Jquery.js; Or use the following statement block:  (function ($) {$ (' #msg '). Hide ()//This is used in this statement block for the $.} defined in Jquery.js (JQuery)   If in the second 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:  <</SPAN&GT Script src = "jquery.js" type = "Text/javascript"/>  <</span> script src = "prototype.js" type = " Text/javascript "/>  <</span> script type =" Text/javascript ">  (function ($) {$ (' #msg '). Hide (); In this statement block, all of the $ defined in Jquery.js are used. (JQuery)   </</span> 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.      Ps:jquery The meaning of special characters:  # indicates ID  . Indicates class   * Select all  , multiple selection   space descendant   > sub   ~ Brother   + Next  : Sub (multifunction)   (function) Filtration and lookup    

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.