jQuery命名冲突解决的五种方案

引言:javascript

最近遇到个问题,同时引用了jquery库和另一个js库。当用$XX去调用js库函数时,发现失效了!因而找资料,原来是jquery命名冲突了。由于许多JavaScript 库使用$做为函数或变量名,jquery也同样。其实$只是jquery的一个别名而已,假如咱们须要使用jquery 以外的另外一js库,咱们能够经过调用 $.noConflict() 向该库返回控制权。下面是收集到解决这一问题的五种方案,总有一种你会用得上的。html

例一:java

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  5. <title>冲突解决1</title> 
  6. <!-- 引入 prototype  --> 
  7. <script src="prototype-1.6.0.3.js" type="text/javascript"></script> 
  8. <!-- 引入 jQuery  --> 
  9. <script src="http://www.cnblogs.com/scripts/jquery-1.3.1.js" type="text/javascript"></script> 
  10. </head> 
  11. <body> 
  12. <p id="pp">test---prototype</p> 
  13. <p >test---jQuery</p> 
  14.  
  15. <script type="text/javascript"> 
  16. jQuery.noConflict();                //将变量$的控制权让渡给prototype.js  
  17. jQuery(function(){                    //使用jQuery  
  18.     jQuery("p").click(function(){  
  19.         alert( jQuery(this).text() );  
  20.     });  
  21. });  
  22.  
  23. $("pp").style.display = 'none';        //使用prototype  
  24. </script> 
  25.  
  26. </body> 
  27. </html> 

例二:jquery

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  5. <title>冲突解决2</title> 
  6. <!-- 引入 prototype  --> 
  7. <script src="prototype-1.6.0.3.js" type="text/javascript"></script> 
  8. <!-- 引入 jQuery  --> 
  9. <script src="http://www.cnblogs.com/scripts/jquery-1.3.1.js" type="text/javascript"></script> 
  10. </head> 
  11. <body> 
  12. <p id="pp">test---prototype</p> 
  13. <p >test---jQuery</p> 
  14.  
  15. <script type="text/javascript"> 
  16. var $j = jQuery.noConflict();        //自定义一个比较短快捷方式  
  17. $j(function(){                        //使用jQuery  
  18.     $j("p").click(function(){  
  19.         alert( $j(this).text() );  
  20.     });  
  21. });  
  22.  
  23. $("pp").style.display = 'none';        //使用prototype  
  24. </script> 
  25. </body> 
  26. </html> 

例三:函数

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  5. <title>冲突解决3</title> 
  6. <!-- 引入 prototype  --> 
  7. <script src="prototype-1.6.0.3.js" type="text/javascript"></script> 
  8. <!-- 引入 jQuery  --> 
  9. <script src="http://www.cnblogs.com/scripts/jquery-1.3.1.js" type="text/javascript"></script> 
  10. </head> 
  11. <body> 
  12. <p id="pp">test---prototype</p> 
  13. <p >test---jQuery</p> 
  14.  
  15. <script type="text/javascript"> 
  16. jQuery.noConflict();                //将变量$的控制权让渡给prototype.js  
  17. jQuery(function($){                    //使用jQuery  
  18.     $("p").click(function(){        //继续使用 $ 方法  
  19.         alert( $(this).text() );  
  20.     });  
  21. });  
  22.  
  23. $("pp").style.display = 'none';        //使用prototype  
  24. </script> 
  25.  
  26. </body> 
  27. </html> 

例四:ui

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  5. <title>冲突解决4</title> 
  6. <!-- 引入 prototype  --> 
  7. <script src="prototype-1.6.0.3.js" type="text/javascript"></script> 
  8. <!-- 引入 jQuery  --> 
  9. <script src="http://www.cnblogs.com/scripts/jquery-1.3.1.js" type="text/javascript"></script> 
  10. </head> 
  11. <body> 
  12. <p id="pp">test---prototype</p> 
  13. <p >test---jQuery</p> 
  14.  
  15. <script type="text/javascript"> 
  16. jQuery.noConflict();                //将变量$的控制权让渡给prototype.js  
  17. (function($){                        //定义匿名函数并设置形参为$  
  18.     $(function(){                    //匿名函数内部的$均为jQuery  
  19.         $("p").click(function(){    //继续使用 $ 方法  
  20.             alert($(this).text());  
  21.         });  
  22.     });  
  23. })(jQuery);                            //执行匿名函数且传递实参jQuery  
  24.  
  25. $("pp").style.display = 'none';        //使用prototype  
  26. </script> 
  27.  
  28. </body> 
  29. </html> 

例五:this

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  5. <title>冲突解决5</title> 
  6. <!--先导入jQuery --> 
  7. <script src="http://www.cnblogs.com/scripts/jquery-1.3.1.js" type="text/javascript"></script> 
  8. <!--后导入其余库 --> 
  9. <script src="prototype-1.6.0.3.js" type="text/javascript"></script> 
  10. </head> 
  11. <body> 
  12. <p id="pp">test---prototype</p> 
  13. <p >test---jQuery</p> 
  14.  
  15. <script type="text/javascript"> 
  16. jQuery(function(){   //直接使用 jQuery ,没有必要调用"jQuery.noConflict()"函数。  
  17.     jQuery("p").click(function(){        
  18.         alert( jQuery(this).text() );  
  19.     });  
  20. });  
  21.  
  22. $("pp").style.display = 'none'; //使用prototype  
  23. </script> 
  24. </body> 
  25. </html>
相关文章
相关标签/搜索