Home >Web Front-end >JS Tutorial >How to resolve $naming conflicts between Jquery library and other libraries_jquery
First of all, we should know that in jquery, $ (dollar sign) is the alias of jquery, which means that using $ is the same as using jquery. In many cases, when we namespace, it is precisely because of this $ conflict occurs. For example: although $('#xmlas') and JQuery('#xmlas') are written differently, they are actually completely equivalent.
To resolve this conflict, the easiest way is to name it with a different name, or let the executing code think it is a different namespace.
1. Import the jQuery library before other libraries, and directly use the jQuery (callback) method such as:
test---jQuery
2. Import the jQuery library after other libraries, and use the jQuery.noConflict() method to transfer control of the variable $ to other libraries. There are several ways:
Code 2
Code 3
In addition to the above method, we can also use the second method to solve the conflict problem, which is the stupidest but most effective solution: use a custom namespace to avoid conflicts.
For example, if the required project name is xmlas, then our original code:
can be written in the following form:
Of course, you can also abbreviate it in the following form:
From this, the complete code implemented according to the first method is as follows:
Of course, you can also simplify the complete code above. The simplified code is as follows: