NoConflict () method of jQuery

Source: Internet
Author: User
Tags custom name

NoConflict () method of jQuery
JQuery core-noConflict () method, run this function to assign control of variable $ to the first library to implement it. This helps ensure that jQuery does not conflict with $ objects in other libraries. NoConflict () method source code: copy the code noConflict: function (deep) {if (window. $ === jQuery) {window. $ =_$;} if (deep & window. jQuery === jQuery) {window. jQuery = _ jQuery;} return jQuery;}, copy the Code definition and usage 1. the noConflict () method transfers jQuery control of variable $. 2. This method releases jQuery's $ variable control. 3. This method can also be used to specify a new custom name for the jQuery variable. Tip: This method is useful when other JavaScript libraries use $ for their functions. Syntax jQuery. noConflict (removeAll) parameter: removeAll Boolean value. Indicates whether jQuery variables can be completely restored. Why is this method used? Many JavaScript libraries use $ as the name of a function or variable, as does jQuery. In jQuery, $ is just the alias of jQuery, so all functions can be guaranteed even if $ is not used. If we need to use another JavaScript library other than jQuery, we can call $. noConflict () returns control to the database: <script type = "text/javascript" src = "other_lib.js"> </script> <script type = "text/javascript" src = "jquery. js "> </script> <script type =" text/javascript "> $. noConflict (); // use the $ code of another library </script> and. the ready () method is used in combination to create aliases for jQuery objects. This technology is very effective: copy the Code <script type = "text/javascript" src = "other_lib.js"> </script> <script type = "text/javascript" sr C = "jquery. js "> </script> <script type =" text/javascript "> $. noConflict (); jQuery (document ). ready (function ($) {// use jQuery $ code}); // use $ code from other libraries </script>. In addition, by passing the true parameter to this method, we can return the control of $ and jQuery to the original library. Please be clear before using it! This is a more challenging version than the simple noConflict method, because it will completely redefine jQuery. This is usually used in extreme cases, such as embedding jQuery in a highly conflicting environment. Note: after this method is called, the plug-in may become invalid. The following example shows how to use the noConflict () method: Example 1: map the referenced object to the original object: jQuery. noConflict (); jQuery ("div p "). hide (); // use jQuery $ ("content "). style. display = "none"; // $ () Example 2 of using another database: Restore the alias $, and then create and execute a function, $ is still used as the alias of jQuery in the scope of this function. In this function, the original $ object is invalid. This function is very effective for most plug-ins that do not depend on other libraries: copy the code jQuery. noConflict (); (function ($) {$ (function () {// use $ as the code for jQuery alias}) ;}( jQuery );... // sample code for copying code from other libraries that use $ as an alias 3: jQuery. noConflict () is combined with the abbreviated ready to make the code more compact: jQuery. noConflict () (function () {// use jQuery code });... // code example 4 for using $ as an alias in other libraries: Create a new alias to use jQuery object: var j = jQuery in the following libraries. noConflict (); j ("div p "). hide (); // jQuery-based code $ ("content "). style. display = "none"; // code example 5 of $ () based on other libraries: Move jQuery to a new namespace completely: 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

Related Article

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.