Solve the conflict between the jquery $ name operator and other frameworks.

Source: Internet
Author: User

Jquery provides a noconfilict API to resolve conflicts.

Usage:

Jquery. noconflict (): Run this function to assign control of variable $ to the first library to implement it.

Jquery. noconflict (true): returns control of $ and jquery to the original library.

First:

 
Jquery. noconflict ();//Do something with jqueryJquery ("Div P"). Hide ();//Do something with another library's $ ()$ ("Content"). style. Display = 'none ';

Second:

Jquery. noconflict ();(Function($) {$ (Function(){//More code using $ as alias to jquery}) ;}) (Jquery );//Other code using $ as an alias to the other library

Third:

 
Jquery. noconflict ()(Function(){//Code using jquery});//Other code using $ as an alias to the other library

Fourth:

VaRJ =Jquery. noconflict ();//Do something with jqueryJ ("Div P"). Hide ();//Do something with another library's $ ()$ ("Content"). style. Display = 'none ';

Category 5:

VaRDOM ={}; Dom. Query= Jquery. noconflict (True);//Do something with the new jqueryDom. Query ("Div P"). Hide ();//Do something with another library's $ ()$ ("Content"). style. Display = 'none';//Do something with another version of jqueryJquery ("div> P"). Hide ();

Noconfilict source code:

Noconflict:Function(Deep ){If(Window. $ =Jquery) {window. $=_ $ ;}If(Deep & window. jquery =Jquery) {window. jquery=_ Jquery ;}ReturnJquery ;},

It is defined at the beginning of jquery source code:

(Function(){VaR//_ Jquery =Window. jquery, _ $=Window. $,//})();
To prevent $ and from being overwritten by jquery, save.

When deep is empty, the regular function of "_ $" to overwrite "window. $" is invalid, but jquery can continue to use it. When "$" is redefined in a new library, "jquery" continues to be a common function of jqquery, and "$" is not in jquery, it is a common function of the new database;

When deep is not empty, it overwrites "_ jquery" to "window. jquery", which may invalidate the jquery plug-in. In addition, the jquery returned by the method is not actually overwritten. It can be used to move to a new namespace, suchDom. query = jquery. noconflict (true); Dom. Query ("Div P"). Hide ();

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.