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