jQuery noConflict() method
jQuery - noConflict() method
How to use jQuery and other frameworks on the page at the same time?
jQuery and other JavaScript frameworks
As you have already learned, jQuery uses the $ sign as Abbreviation 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 (just like jQuery). 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
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $.noConflict(); jQuery(document).ready(function(){ jQuery("button").click(function(){ jQuery("p").text("jQuery 仍然在工作!"); }); }); </script> </head> <body> <p>这是一个段落。</p> <button>点我</button> </body> </html>
Running Example »Click the "Run Example" button to view the online example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> var jq=$.noConflict(); jq(document).ready(function(){ jq("button").click(function(){ jq("p").text("jQuery 仍然在工作!"); }); }); </script> </head> <body> <p>这是一个段落。</p> <button>点我</button> </body> </html>
Run Instance»Click the "Run Instance" button to view the online instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $.noConflict(); jQuery(document).ready(function($){ $("button").click(function(){ $("p").text("jQuery 仍然在工作!"); }); }); </script> </head> <body> <p>这是一个段落。</p> <button>点我</button> </body> </html>
Run Instance»Click the "Run Instance" button to view the online instance