Examples of noConflict () usage in jQuery, jquerynoconflict

Source: Internet
Author: User

Examples of noConflict () usage in jQuery, jquerynoconflict

This article describes the usage of noConflict () in jQuery. Share it with you for your reference. The specific analysis is as follows:

JQuery uses the "$" operator by default. The $ symbol is only window. A reference of the jQuery object, jQuery. noConflict (), which transfers control of variable $ to the first database that implements it. This helps ensure that jQuery does not conflict with $ objects in other libraries. After running this function, you can only use jQuery variables to access jQuery objects. For example, to use $ ("div p"), you must replace it with jQuery ("div p ").

I. "$" Operator

1. jQuery uses the "$" operator by default. Other frameworks such as prototype also use "$". Therefore, if jQuery is introduced after other libraries, jQuery will get the "$" permission. This situation is easy to understand. After all, JavaScript is executed from top to bottom.

2. If jQuery is introduced before other libraries that use "$", jQuery will not use "$ ".

Tip: This method is useful when other JavaScript libraries use $ for their functions.

$ Is used for getting variables in jquery, but there are still many plug-ins that can use the $ symbol. If we want to reference the variable at the same time, a problem will occur, jquery introduces noconflict () to prevent such a problem ()

Ii. jQuery. noConflict Definition
 
The jQuery. noConflict method contains an optional Boolean parameter to determine whether to hand over the jQuery object itself while transferring the $ reference:

jQuery.noConflict([removeAll])

Function Description:

By default, executing noConflict will hand over control of variable $ to the first database that generates $. When removeAll is set to true, executing noConflict will hand over all the control of $ and jQuery object to the first one to generate their libraries.
 
Iii. jQuery. noConflict source code analysis
 
At the beginning of jQuery source code, the first thing we do is:

// Map over jQuery in case of overwrite_jQuery = window.jQuery,// Map over the $ in case of overwrite_$ = window.$,

It is easy to understand that jQuery maps jQuery and $ objects in the window environment through two private variables to prevent variables from being forcibly overwritten. Once the noConflict method is called, the transfer method of control is determined by the difference between _ jQuery, _ $, jQuery, and $. The specific code is as follows:

noConflict: function( deep ) { if ( window.$ === jQuery ) { window.$ = _$; } if ( deep && window.jQuery === jQuery ) { window.jQuery = _jQuery; } return jQuery;}

Let's take a look at the parameter setting problem mentioned above. If deep is not set, _ $ overwrites window. $. At this time, jQuery alias $ is invalid, but jQuery itself is intact. If another class library or Code redefined the $ variable, its control is completely handed over. If deep is set to true, _ jQuery overwrites window. jQuery, and $ and jQuery both expire.
The advantage of this operation is that, whether the frameworks are mixed or multiple versions of jQuery coexist in a highly conflicting execution environment, due to the transfer mechanism provided by the noConflict method and the unoverwritten jQuery object returned by itself, it can completely resolve conflicts by means of variable ing.

Iv. jQuery. noConflict instance
 
1. Map $ referenced objects back to the original objects:

JQuery. noConflict (); jQuery ("div p "). hide (); // use jQuery $ ("content "). style. display = "none"; // use $ () of other libraries ()

2. Restore the alias $, create and execute a function, and use $ as the alias of jQuery in the function scope. In this function, the original $ object is invalid. This function is very effective for most plug-ins that do not depend on other libraries:

JQuery. noConflict (); (function ($) {$ (function () {// use $ as the code for jQuery alias}) ;}( jQuery );... // other code of the database that uses $ as the alias

3. jQuery. noConflict () can be combined with the abbreviated ready to make the code more compact:

JQuery. noConflict () (function () {// use jQuery code});... // code for alias using $ in other libraries

4. Create a new alias to use the jQuery object in the following Library:

Var j = jQuery. noConflict (); j ("div p "). hide (); // jQuery-based code $ ("content "). style. display = "none"; // $ () code based on other libraries

5. Completely move jQuery to a new namespace:

Var dom ={}; dom. query = jQuery. noConflict (true); // result: dom. query ("div p "). hide (); // new jQuery code $ ("content "). style. display = "none"; // jQuery ("div> p") Code of another library $ "). hide (); // jQuery code of another version

I hope this article will help you with jQuery programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.