Home > Web Front-end > JS Tutorial > body text

In-depth understanding of the noConflict() method in jQuery

yulia
Release: 2018-09-26 15:21:47
Original
1814 people have browsed it

This article introduces you to the noConflict() method in jQuery. Friends in need can refer to it. I hope it will be helpful to you. Likes and comments are also welcome.

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 reason, jQuery provides the noConflict() method

Release the identifier

will release control of the $identifier

In this way, other scripts can use

$.noConflict();
Copy after login

full name instead of

Use jQuery full name instead of abbreviation $

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

Create abbreviation

You can create your own abbreviation

noConflict() can return a reference to jQuery

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

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

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 仍然在工作!");
  });
});
Copy after login
outside the function.

The above is the detailed content of In-depth understanding of the noConflict() method in jQuery. For more information, please follow other related articles on the PHP Chinese website!

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