Effective Solution to jquery version conflicts _ jquery

Source: Internet
Author: User
This article mainly introduces effective methods to solve jquery version conflicts, which is of great reference for compatibility debugging of web design, anyone who needs jQuery can refer to those who have used jQuery knows that different jQuery versions may cause conflicts. This article provides an effective solution to this problem as follows:

Case: solve the conflict between jQuery1.3.2 and 1.4.2. (This example has passed the test !)

Step 1: Add the following sentence at the end of the source code 1.4.2:

var $j4 = jQuery.noConflict(true);

The reason why I add the source code here is not to add it when I use it as mentioned in most articles. This is because many 1.4.2-based plug-ins need to be added, adding too many plug-ins can avoid repeated code. In this statement, the reference permissions of jQuery and $ of 1.4.2 are all waived. That is, plug-ins Based on 1.4.2 cannot use jQuery and $. At the same time, a new namespace for $ j4 is given. Note that it is the property of window. Looking at the source code of 1.4.2, we can see that it actually runs the following two sentences:

window.$=_$;window.jQuery=_jQuery;

The same reason is that window. $ = _ temp $ (return namespace) is named differently.

Step 2: Add the following code to the header of all plug-ins Based on the 1.4.2 framework:

var _temp$ = window.$,_tempjQuery = window.jQuery;

Place $ and jQuery of jQuery1.3.2 in the temporary variable space:

window.$ = $j4;

This sentence and the following sentence are used to correctly use jQuery and $ for the intermediate code. The following $ j4 gives them correct references.

window.jQuery = $j4;

There are three reasons for doing so:

①. We do not want to change the source code of a large number of jQuery plug-ins. It is best not to change the source code. If you do not change the source code, try to change it as little as possible. Adding Code changes at the end of the header makes the original code in the middle a good way.

②. Because 1.4.2 has abandoned the control of jQuery and $, but the existing plug-in code uses them for reference again, because the plug-in cannot predict conflicts, even if there is a conflict, the plug-in developed by others must be referenced by $ or jQuery unless it is not a plug-in under jQuery.

③. In order to prevent the plug-in from directly referencing with window. $ and window. jQuery, The jQuery and $ of 1.3.2 are referenced. Although this situation is rare, it should be avoided.

The original code in the middle does not move, and the code is added at the end:

Window. $ = _ temp $; // return $'s reference permission to jQuery1.3.window. jQuery = _ tempjQuery; // return jQuery's reference permission to jQuery1.3.

Step 3: Use the jQuery1.4.2-based selection function to use only $ j4 (element.

Conclusion: To date, jQuery1.4.2 has completely waived the control permissions of $ and jQuery. 1.3.2 give up $'s control permission, but do not give up jQuery's permission. In fact, jQuery can also give up, just give an alias $ j3. It is recommended that prototype be placed after jQuery1.3.2, which grants $ control permissions. You must use $ j4 to reference jQuery1.4.2 later. However, even if there are more jQuery Framework Version conflicts, they are all solved. Let's take a look at how to use jQuery 1.2. Refer to the execution steps in (2), but the first step is changed:

var $j2 = jQuery.noConflict(true);

Step 3: use $ j2 (element. The truth is the same.

I believe this article provides some reference value for jQuery program design.

Related Article

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.