Use jquery. noconflict () to prevent $ conflict

Source: Internet
Author: User
Jquery. noconflict () function,

Run this function to assign control of variable $ to the first database to implement 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 ").

Note:After you import the jquery file,In addition, when importing another library that causes conflictsBeforeUse(Luckymien note: the content in the lower part is inaccurate. This is not required in the new version of jquery ). Of course, it should also be before other conflicting libraries are used (luckymien Note: This sentence is undoubtedly correct), unless jquery is the last imported.

Example:

Maps $ referenced objects back to the original objects.

JqueryCode:

Jquery. noconflict ();
// Use jquery
Jquery ("Div P"). Hide ();
// Use $ () for other libraries ()
$ ("Content"). style. Display = 'none ';

Resume the use of Alias $, and then create and execute a function. In the scope of this function, $ is still used as the alias of jquery. 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 code:

 
Jquery. noconflict ();
(Function($){
$ (Function(){
// Use $ as the jquery alias code
});
}) (Jquery );
// Other code of the database that uses $ as the alias

Create a new alias to use the jquery object in the following library.

Jquery code:

 
VaRJ = jquery. noconflict ();
// Jquery-based code
J ("Div P"). Hide ();
// $ () Code based on other libraries
$ ("Content"). style. Display = 'none ';

Luckymien added: jquery. noconflict () has a deep parameter. If it is true, it can not only prevent the $ conflict, but also prevent the keyword conflict of jquery.

Jquery code:

VaRJ = jquery. noconflict (true );
// Jquery-based code
J ("Div P"). Hide ();
// $ () Code based on other libraries
$ ("Content"). style. Display = 'none ';

// Jquery () code based on other libraries (or versions)
Jquery ("# ABC" ).html ("this is rare! ");

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.