Home > Web Front-end > JS Tutorial > body text

What to do if different jquery versions conflict

coldplay.xixi
Release: 2020-11-30 15:17:00
Original
1885 people have browsed it

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);
Copy after login

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;
Copy after login

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;
Copy after login

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

window.$ = $j4;
Copy after login

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;
Copy after login

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.
Copy after login

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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!