Home  >  Article  >  Web Front-end  >  What to do if different jquery versions conflict

What to do if different jquery versions conflict

coldplay.xixi
coldplay.xixiOriginal
2020-11-24 11:59:241890browse

Solution to conflicts between different jquery versions: first add relevant code at the end of the source code of [1.4.2]; then add relevant code to the head of all plug-ins based on the framework of [1.4.2] Code; finally, if you want to use the selection function based on [jQuery1.4.2], use [$j4(element)].

What to do if different jquery versions conflict

The operating environment of this tutorial: windows7 system, jQuery1.3.2&&jQuery1.4.2 version. This method is suitable for all brands of computers.

Solutions to conflicts between different jquery versions:

Case: Resolve conflicts between jQuery 1.3.2 and 1.4.2.

The first step: 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 the big one Most articles mention 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;

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

window.$ = $j4;

This sentence and the following sentence are all for the intermediate 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. It’s best not to leave it alone, even if 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 reference jQuery and $ in 1.3.2, although this It's rare, but 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.

I believe that what is described in this article has certain reference value for everyone’s jQuery programming.

Related free learning recommendations: JavaScript (video)

The above is the detailed content of What to do if different jquery versions conflict. 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