Solve the Problem of jquery-China-US meta-symbol naming conflict _ jquery-js tutorial

Source: Internet
Author: User
In Jquery, $ is the alias of JQuery. You can use JQuery to replace $, the following describes how to solve the naming conflict between jquery and China. In Jquery, $ is the alias of JQuery. JQuery can also be used to replace $, for example, $ ('# msg') is equivalent to JQuery ('# msg. However, when we introduce multiple js libraries and define the $ symbol in another js library, we will encounter a conflict when using the $ symbol. The following describes how to introduce two library files: jquery. js and prototype. js.

First case: jquery. js is introduced after prototype. js, for example: <script src = "prototype. js" type = "text/javascript"/>
<Script src = "jquery. js" type = "text/javascript"/>
In this case, we write the following in our own js Code:
$ ('# Msg'). hide ();
$ Always represents the $ symbol defined in jquery, and can also be written as JQuery ('# msg '). hide (); if you want to use prototype. $ defined in js. We will introduce it later.

Case 2: jquery. js is introduced before prototype. js, for example: <script src = "jquery. js" type = "text/javascript"/>
<Script src = "prototype. js" type = "text/javascript"/>
In this case, we write the following in our own js Code:
$ ('# Msg'). hide ();
$ Prototype. $ symbol defined in js. If we want to call jquery. if the factory in js selects the function, it can only be written in the full name JQuery ('# msg '). hide ().

The following describes how to correctly use the $ symbol defined in different js libraries when the file sequence of the first js library is introduced.

I. Use JQuery. noConflict ()
The function of this method is to let Jquery give up its ownership of $ and return the control of $ to prototype. js, because jquery. js is introduced later, so jquery is the final control of $. The returned value is JQuery. After the method is called in the code, we cannot use $ to call the jquery method. $ represents the $ defined in the prototype. js library. JQuery. noConflict ();
// You cannot write $ ('# msg'). hide () here. $ represents the $ symbol defined in prototype. js.
JQuey ('# msg'). hide ();
Since then, $ represents the $ defined in prototype. js. $ in jquery. js cannot be used any more. You can only use the full name of $ in jquery. js.

Ii. Customize the alias of JQuery
If the JQuery. noConflict () method is used in the first method, you can only use the full name of JQuery. We can also redefine the alias for JQuery. As follows:
Var $ j = JQuery. noConflict ();
$ J ('# msg'). hide (); // $ j stands for JQuery
Since then, $ represents the $ defined in prototype. js. $ in jquey. js cannot be used any more. You can only use $ j as the JQuery alias in jquey. js.

3. Use the statement block. The $ defined in jquery. js is still used in the statement block, as shown below:
JQuery. noConflict ();
JQuery (document ). ready (function ($) {$ ('# msg '). hide (); // $ used in the method of the entire ready event is jquery. $.} defined in js .}); or use the following statement blocks:
(Function ($) {$ ('# msg '). hide (); // jquery is used in this statement block. $.} defined in js .}) (JQuery)
If $ in jquery. js is used when the file sequence of the js library is introduced in the second method, we can still use the statement block method described above, for example:
<Script src = "jquery. js" type = "text/javascript"/>
<Script src = "prototype. js" type = "text/javascript"/>
<Script type = "text/javascript">
(Function ($) {$ ('# msg '). hide (); // jquery is used in this statement block. $.} defined in js .}) (JQuery)

This method of using statement blocks is very useful. We should use this method when writing jquery plug-ins by ourselves, because we do not know how to introduce various js libraries in sequence during our work, however, the syntax of such statement blocks can avoid conflicts.

Ps: the meaning of special characters in jquery:
# Id
. Indicates class
* Select all
, Select multiple
Space descendant
> Sub-
~ Brother
+ Next
: Sub-function)
() Functional 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.