Solve the problem of the dollar sign naming conflict in jquery _jquery

Source: Internet
Author: User
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.

In the first case: Jquery.js is introduced after prototype.js, such as: <</span> script src = "prototype.js" type = "Text/javascript"/>
<</span> 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: <</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:
$ (' #msg '). Hide ();
$ symbol defined in the prototype.js at this time, if we want to call the factory selection function in Jquery.js, we can only use jquery (' #msg ') with the full name. Hide ().

The following first introduced in the first JS library file order, how to correctly use the different JS library defined in the $ symbol.

I. Use of JQUERY.NOCONFLICT ()
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 ();
You can no longer write $ (' #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 using the full name jquery in Jquery.js.

Two. Customizing the alias for jquery
If you think that after using the Jquery.noconflict () method in the first method, you can only use the jquery full name for more trouble, 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 $ is no longer available on behalf of the $ $,jquey.js defined in Prototype.js, only $j can be used as an alias for jquery in Jquey.js.

Three. Using the statement block, the $ defined in Jquery.js is still used in the statement block, as follows:
Jquery.noconflict ();
JQuery (document). Ready (function ($) {$ (' #msg '). Hide ();//The $ in the method for the entire ready event is defined in Jquery.js as $.}); Or use the following statement block:
(function ($) {$ (' #msg '). Hide ()//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> script src = "jquery.js" type = "Text/javascript"/>
<</span> script src = "prototype.js" type = "Text/javascript"/>
<</span> script type = "Text/javascript" >
(function ($) {$ (' #msg '). Hide ()//is used in this statement block for the $.} defined in Jquery.js (JQuery)
</</span> script >
This use of block method is very useful, in our own jquery plug-in, should be used in this way, because we do not know how the specific work process is the sequence of the introduction of a variety of JS library, and this sentence block writing can shield conflict.

The meaning of special characters in Ps:jquery:
# indicates ID
. Indicates class
* Select All
, multiple selection
Space descendants
> Son
~ Brother
+ Next
: Sub (multifunction)
() function filtering and searching

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.