Home  >  Article  >  Web Front-end  >  Learn how to use noConflict() in jQuery

Learn how to use noConflict() in jQuery

青灯夜游
青灯夜游forward
2018-10-08 16:58:061778browse

In this article, we have compiled relevant knowledge points about learning the usage of noConflict() in jQuery. Friends who need it can learn it.

noConflict()

jQuery uses the $ symbol as the abbreviation of jQuery

JavaScript frameworks include: MooTools, Backbone, Sammy, Cappuccino, Knockout, JavaScriptMVC, Google Web Toolkit, Google Closure, Ember, Batman and ExtJS, etc.

Some of these frameworks also use the $ symbol as abbreviation

If two different frameworks are used, use the same abbreviation symbol, May cause the script to stop running

For this purpose, jQuery provides the noConflict() method

Release the identifier

will release control of the $identifier

In this way, other scripts can use

$.noConflict();

full name instead of

Use jQuery full name instead of abbreviation $

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

Create abbreviations

You can create your own abbreviations

noConflict() can return a reference to jQuery

Save it into a variable, and then, Use this variable instead of $

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

Parameter passing

You can use the $ symbol as a variable and pass it to the ready method

In this way, you can pass it inside the function Use the $ symbol. You cannot use $

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

The above is the detailed content of Learn how to use noConflict() in jQuery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete