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

Solve the problem of jquery plug-in conflict_javascript skills

WBOY
Release: 2016-05-16 17:02:53
Original
1167 people have browsed it

I made an example today, using jquery and jquery plug-in lightbox to create a floating magnification effect for images without any problems.

Then I added a js for the navigation bar. The js for the structural navigation and the js for floating and enlarging images conflicted.

Either only the navigation bar js is valid, or only the image floating js is valid.

After searching online, the solution is as follows

jQuery.noConflict()

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 may cause conflicts. Of course it should also be done before other conflicting libraries are used, unless jQuery is the last one imported. Example

Description:
Map the object referenced by $ back to the original object.

jQuery code:
jQuery.noConflict();
// Use jQuery
jQuery("div p").hide();
// Use other Library's $()
$("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:
jQuery.noConflict();
(function($) {
$(function() {
// Use $ as jQuery alias Code
});
})(jQuery);
// Code description for other libraries that use $ as alias:
Create a new alias to use jQuery in the next library object.

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


jQuery.noConflict();
(function($) {
$(function() {
// Code that uses $ as jQuery alias I put the navigation js Just write the code in the middle
});
})(jQuery);
// Code for other libraries that use $ as aliases

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!