jQuery基础学习3——jQuery库冲突

默认情况下,jQuery用$作为自身的快捷方式。

  1. jQuery库在其他库之后导入

在其他库和jQuery库都被加载完毕后,可以在任何时候调用jQuery.noConflict()函数来将变量$的控制权移交给其他JavaScript库。

 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="lib/prototype.js" type="text/javascript"></script>
 8         <!-- 引入 jQuery  -->
 9         <script src="../../scripts/jquery.js" type="text/javascript"></script>
10     </head>
11     <body>
12         <p id="pp">Test-prototype(将被隐藏)</p>
13         <p >Test-jQuery(将被绑定单击事件)</p>
14         <script type="text/javascript">
15             jQuery.noConflict();                //将变量$的控制权让渡给prototype.js
16             jQuery(function(){                    //使用jQuery
17                 jQuery("p").click(function(){
18                     alert( jQuery(this).text() );
19                 });
20             });
21 
22             $("pp").style.display = 'none';        //使用prototype.js隐藏元素
23         </script>
24     </body>
25 </html>


可以在程序里将jQuery()函数作为jQuery对象的制造工厂。


如果想确保jQuery不会与其他库冲突,但又想自定义一个快捷方式,可以使用如下的操作:

1         var $j = jQuery.noConflict();        //自定义一个比较短快捷方式
2         $j(function(){                        //使用jQuery
3             $j("p").click(function(){
4                 alert( $j(this).text() );
5             });
6         });
7 
8         $("pp").style.display = 'none';        //使用prototype.js隐藏元素

如果不想给jQuery自定义备用名称,还想使用$而不管其他库的$()方法,同时又不想与其他库相冲突,那么可以使用下面的解决方法:

1         jQuery.noConflict();                //将变量$的控制权让渡给prototype.js
2         jQuery(function($){                    //使用jQuery设定页面加载时执行的函数,重点是把$作为参数传递给了函数
3             $("p").click(function(){        //继续使用 $ 方法
4                 alert( $(this).text() );
5             });
6         });
7 
8         $("pp").style.display = 'none';        //使用prototype

 

 1 jQuery.noConflict();                //将变量$的控制权让渡给prototype.js
 2         (function($){                        //定义匿名函数并设置形参为$
 3             $(function(){                    //匿名函数内部的$均为jQuery
 4                 $("p").click(function(){    //继续使用 $ 方法
 5                     alert($(this).text());
 6                 });
 7             });
 8         })(jQuery);                            //执行匿名函数且传递实参jQuery
 9 
10         $("pp").style.display = 'none';        //使用prototype
  1. jQuery库在其他库之前导入

如果jQuery库在其他库之前就导入了,那么可以直接使用“jQuery”来做一些jQuery的工作,同时可以使用$()方法作为其他库的快捷方式,这里无须调用jQuery.onConflict()函数。

 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="../../scripts/jquery.js" type="text/javascript"></script>
 8         <!-- 引入 prototype  -->
 9         <script src="lib/prototype.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>

 

posted on 2015-09-03 16:41  帅胡  阅读(169)  评论(0编辑  收藏  举报

导航