Latest web development tutorials

jQuery noConflict () method

How to use jQuery and other frameworks on the page?


jQuery and other JavaScript frameworks

As you have learned, jQuery use the $ symbol as jQuery shorthand.

If other JavaScript frameworks use the $ symbol as a shorthand for how to do?

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

Some frameworks use the $ symbol as shorthand (like jQuery), if both of you are using a different framework is using the same abbreviations, it may cause the script to stop running.

jQuery team to consider this issue, and realized noConflict () method.


jQuery noConflict () method

noConflict () method will release control will be $ identifier, so that other scripts can use it.

Of course, you can still shorthand for the full name of an alternative way to use jQuery:

Examples

$.noConflict();
jQuery(document).ready(function(){
jQuery("button").click(function(){
jQuery("p").text("jQuery 仍然在工作!");
});
});

try it"

You can also create your own shorthand. noConflict () returns a reference to jQuery, you can put it into a variable for later use. Consider this example:

Examples

var jq = $.noConflict();
jq(document).ready(function(){
jq("button").click(function(){
jq("p").text("jQuery 仍然在工作!");
});
});

try it"

If you use the $ shorthand jQuery code block, and you do not want to change this shortcut, you can put $ symbol passed as an argument to the ready methods. So that you can use the $ symbol in the function - and function outside, still we have to use "jQuery":

Examples

$.noConflict();
jQuery(document).ready(function($){
$("button").click(function(){
$("p").text("jQuery 仍然在工作!");
});
});

try it"