jquery resolves plugin conflicts

Source: Internet
Author: User

In jquery, $ is an alias for jquery, and all uses of $ can be replaced with jquery, such as $ (' #msg ') equivalent to the notation of jquery (' #msg '). However, when we introduced multiple JS libraries and also defined the $ symbol in another JS library, we had a conflict when we used the $ symbol. The following is an example of introducing two library files Jquery.js and prototype.js.

The first scenario: Jquery.js is introduced after prototype.js, such as:

< script src = "prototype.js" type = "Text/javascript"/>  < script src = "jquery.js" type = "Text/java Script "/>

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 $ as defined in Prototype.js, we'll introduce it later.

The second scenario: Jquery.js is introduced before prototype.js, such as:

< script src = "jquery.js" type = "Text/javascript"/>  

In this case, we write in our own JS code as follows: $ (' #msg '). Hide (); $ at this time represents the $ symbol defined in the Prototype.js, If we want to invoke the factory selection function function in Jquery.js, we can only use the full name of jquery (' #msg '). Hide ().

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

I. Using JQUERY.NOCONFLICT () The function of this method is to let jquery give up ownership of $, return the control of $ to prototype.js, because Jquery.js is introduced later, so the last to have control is jquery. Its return value is jquery. When the method is called in code, we cannot use $ to invoke the JQuery method, at which time $ represents the $ defined in the Prototype.js library. As follows:

Since then $ is no longer available on behalf of the $,jquery.js defined in Prototype.js, only the full name of jquery in Jquery.js is used .

Two. Customizing the alias of jquery if you think that using the Jquery.noconflict () method in the first method, you can only use the jquery full name to be more troublesome, we could also redefine the alias for jquery. As follows:

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.

Disadvantage: After the introduction of this code, not only the current JS file, the HTML reference in all JS, if useful to the $ in jquery, you have to use $J to replace the previous $

Three. Theready function is the entry function of jquery, which can be used to replace $ (document) with the (. function () {) with jquery (document). Ready (function ($) {}

Disadvantage: Only the code within the ready nesting is valid, and the code outside of the nesting is not valid. If all your logic is written in the ready function, that's fine. But we usually write some sub-functions outside of the ready function, and then the ready function calls these functions. This scheme is not valid for these functions, so the scheme has limitations.

Four. Using the statement block, the $ defined in Jquery.js is still used in the statement block as follows:(recommended, especially when developing JS plugin)

To a function on the JS content pack

JQuery (function ($) {
Your JS code here (for example, the Ready function and sub-function mentioned in the third scenario)
If it is a JS file, it is in the file header and tail each add a line of code
}or(function ($) {
Your JS code
}) (JQuery);

If in the second Introduction JS library file order, how to use $ in jquery.js, we can still use the statement block described above method, such as:

<script src = "jquery.js" type = "Text/javascript"/> <script src = "prototype.js" type = "Text/javascript"/> <script type= "Text/javascript" > (function ($) {   $ (' #msg '). Hide ();//At this point in the statement block is used in the Jquery.js defined in the $.   }) (JQuery) </script>

This method of using a block of statements is very useful, if you use Scenario 2, then other people in use, you have to abide by your contract, the JS code in your own $ $, and if you use Scenario 4, you can avoid the impact of the conflict on the component, and do not require the use of public components of the person to modify their own code.

We should use this notation when we write our own jquery plugins, because we don't know how to introduce the various JS libraries in the process of the work, and this block of statements can mask conflicts.

jquery resolves plugin conflicts

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.