Home >Web Front-end >JS Tutorial >jQuery dollar sign conflict solution_jquery

jQuery dollar sign conflict solution_jquery

WBOY
WBOYOriginal
2016-05-16 18:31:161128browse

The following code:

Copy the code The code is as follows:

jQuery.noConflict();
jQuery(document).ready(function(){

//Your Code....

});

jQuery.noConflict( ) Detailed introduction:
Overview
Run this function to transfer control of the variable $ to the first library to implement it. This helps ensure that jQuery does not conflict with other libraries' $ objects. After running this function, you can only access jQuery objects using jQuery variables. For example, where $("div p") is used, it must be replaced by jQuery("div p").
Note: This function must be used after you import the jQuery file, and before importing another library that causes conflicts. Of course it should also be done before other conflicting libraries are used, unless jQuery is the last one imported.
Description:
Map the object referenced by $ back to the original object.
jQuery code:
Copy code The code is as follows:

jQuery.noConflict();
// Use jQuery
jQuery("div p").hide();
// Use $() from other libraries
$("content").style.display = 'none ';

Description:
Restore the use of the alias $, and then create and execute a function that still uses $ as an alias for jQuery in the scope of this function. In this function, the original $ object is invalid. This function works well for most plugins that don't rely on other libraries.
jQuery code:
Copy code The code is as follows:

jQuery.noConflict();
(function($) {
$(function() {
// Code using $ as jQuery alias
});
})(jQuery);
// Code for other libraries that use $ as an alias

Description:
Creates a new alias for using jQuery objects in subsequent libraries.
jQuery code:
Copy code The code is as follows:

var j = jQuery.noConflict ();
// Code based on jQuery
j("div p").hide();
// Code based on $() from other libraries
$("content"). style.display = 'none';
Statement:
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