Home  >  Article  >  Web Front-end  >  How to resolve conflicts between different versions of jquery?

How to resolve conflicts between different versions of jquery?

青灯夜游
青灯夜游Original
2020-11-19 15:32:111981browse

How to resolve conflicts between different versions of jquery?

Friends who have used jQuery know that different versions of jQuery can cause conflicts, so how to solve the version conflict problem? The following article will introduce it to you.

Case: Resolve the conflict between jQuery1.3.2 and 1.4.2. (This example has been tested and passed!)

Step one: Add a sentence at the end of the source code of 1.4.2:

var $j4 = jQuery.noConflict(true);

The reason why it is added here in the source code is not like most The article mentions adding it when it is needed. This is because many plug-ins based on 1.4.2 need to be added. Adding it here can avoid duplication caused by adding this code to too many plug-ins. This sentence gives up all the reference permissions of jQuery and $ in 1.4.2. That is to say, plug-ins based on 1.4.2 can no longer use jQuery and $. At the same time, give a new namespace to $j4. Note that it is an attribute of window. If you look at the source code of 1.4.2, you will find that it actually executes these two sentences:

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

The principle is the same as window.$=_temp$ (returning the namespace), but the naming is different.

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

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

Put the $ and jQuery of jQuery1.3.2 into the temporary variable space :

window.$ = $j4;

This sentence and the following sentence are all for the middle code to use jQuery and $ correctly. The following $j4 is to give them the correct reference.

window.jQuery = $j4;

There are three reasons why we need to store temporary variables first:

①. We don’t want to change a large amount of jQuery plug-in source code. The best thing is not to move. Even if you change it, try to change it as little as possible. It is also a good way to add modified code at the head and tail, leaving the original code in the middle unchanged.

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

③. In order to prevent the plug-in from directly using window.$ and window.jQuery to refer to 1.3.2 jQuery and $, although this situation is relatively rare, it is just in case.

The original code in the middle remains unchanged, and the following code is added at the end:

window.$ = _temp$;//将$的引用权限返还给jQuery1.3.
window.jQuery = _tempjQuery;//将jQuery的引用权限返还给jQuery1.3.

Step 3: If you want to use the selection function based on jQuery1.4.2 in the future, you can only use $j4(element).

Summary: The feasible solution so far: jQuery1.4.2 completely gives up the control permissions of $ and jQuery. 1.3.2 Give up the control rights of $ but not the rights of jQuery. In fact, jQuery can also be given up, but it must be given the alias $j3. It is best to place prototype behind jQuery1.3.2, which obtains the control permissions of $. It’s just that if you want to use jQuery1.4.2 in the future, you must use $j4 to reference it. But in this way, no matter how many jQuery framework version conflicts there are, they will all be solved. What if jQuery 1.2 comes, refer to the execution steps of (2), but the first step is changed to:

var $j2 = jQuery.noConflict(true);

The third step uses $j2(element). The principles are the same.

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of How to resolve conflicts between different versions of jquery?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn