Home > Web Front-end > JS Tutorial > The noConflict() method in jQuery uses _jquery

The noConflict() method in jQuery uses _jquery

WBOY
Release: 2016-05-16 17:35:30
Original
1181 people have browsed it

jQuery and other JavaScript frameworks
As you already know, jQuery uses the $ sign as shorthand for jQuery.

What if other JavaScript frameworks also use the $ symbol as a shorthand?

Some other JavaScript frameworks include: MooTools, Backbone, Sammy, Cappuccino, Knockout, JavaScript MVC, Google Web Toolkit, Google Closure, Ember, Batman, and Ext JS.

Some of these frameworks also use the $ symbol as a shorthand (like jQuery), and if you are using two different frameworks that are using the same shorthand symbol, it may cause the script to stop running.

The jQuery team took this issue into consideration and implemented the noConflict() method.

jQuery noConflict() Method The
noConflict() method releases control of the $ identifier so that other scripts can use it.

Example
Of course, you can still use jQuery by replacing the abbreviation with the full name:

Copy code Code As follows:

$.noConflict();
jQuery(document).ready(function(){
jQuery("button").click(function(){
jQuery("p").text("jQuery is still running!");
});
});

Example
You can also create your own abbreviation . noConflict() returns a reference to jQuery that you can store in a variable for later use. Please look at this example:
Copy code The code is as follows:

var jq = $.noConflict( );
jq(document).ready(function(){
jq("button").click(function(){
jq("p").text("jQuery is still running! ");
});
});

Example
If your jQuery code block uses $ shortcut and you don't want to change this shortcut, then you can Pass the $ sign as a variable to the ready method. This way you can use the $ symbol inside the function - but outside the function, you still have to use "jQuery":
Copy code The code is as follows:

$.noConflict();
jQuery(document).ready(function($){
$("button").click(function(){
$("p").text("jQuery is still running!");
});
});
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