Jquery.noconflict () method

Source: Internet
Author: User

Definition and usage of the Jquery.noconflict () method:

This method can transfer the control of $ to the other first library that implements it.

In practical applications, it is possible that the other JS library also has the use of $, which may lead to conflicts with jquery, so using the Jquery.noconflict () method can effectively prevent this kind of conflict.

Syntax structure:

Syntax One:

Jquery.noconflict ()

Transfer the control of $ to the other first library that implements it, at which time it is not possible to use $, but to use jquery.

code example:

<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><Metaname= "Author"content= "http://www.51texiao.cn/" /><title>Ant Tribe</title><Scriptsrc= "Http://libs.baidu.com/jquery/1.9.0/jquery.js"></Script><Scripttype= "Text/javascript">$ (document). Ready (function() {jquery.noconflict (); JQuery (". First"). CSS ("Color","Red"); $(". Second"). CSS ("Color","Green");})</Script></Head><Body><Divclass= "First">Ant Tribe</Div><Divclass= "Second">Ant Tribe</Div></Body></HTML>

The above code can be seen, $ has expired.

You can also create an alias for jquery to use the functions in the jquery library. For example:

var myjq=jquery.noconflict ();

Full code:

<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><Metaname= "Author"content= "http://www.51texiao.cn/" /><title>Ant Tribe</title><Scriptsrc= "Http://libs.baidu.com/jquery/1.9.0/jquery.js"></Script><Scripttype= "Text/javascript">$ (document). Ready (function(){  varMYJQ=jquery.noconflict (); MYJQ (". First"). CSS ("Color","Red"); JQuery (". Second"). CSS ("Color","Green");})</Script></Head><Body><Divclass= "First">Ant Tribe</Div><Divclass= "Second">Ant Tribe</Div></Body></HTML>

You can use either "Alias" or "JQuery" in your code.

Syntax Two:

Jquery.noconflict (Extreme)

Parameter list:

Parameters Describe
Extreme A Boolean value of true. You can move jquery completely to a new namespace.

For example:

var myjq=jquery.noconflict (true);

The jquery function can only be used at this time using MYJQ.

Full code:

<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><Metaname= "Author"content= "http://www.51texiao.cn/" /><title>Ant Tribe</title><Scriptsrc= "Http://libs.baidu.com/jquery/1.9.0/jquery.js"></Script><Scripttype= "Text/javascript">$ (document). Ready (function(){  varMYJQ=Jquery.noconflict (true); MYJQ (". First"). CSS ("Color","Red"); JQuery (". Second"). CSS ("Color","Green");})</Script></Head><Body><Divclass= "First">Ant Tribe</Div><Divclass= "Second">Ant Tribe</Div></Body></HTML>

The above code shows that if the parameter of the method is true, then "jquery" is not used, only the new namespace can be used to refer to the jquery function.

The original address is: http://www.51texiao.cn/jqueryjiaocheng/2015/0523/2390.html

The original address is: http://www.softwhy.com/

Jquery.noconflict () method

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.