与其他库一起使用jQuery

概要:
  在jQuery库,几乎所有的jQuery插件都被约束在它的命名空间里,通常,“global”对象同样被存储在jQuery命名空间里,因些不会使它与其它库(如:Prototype, MooTools, or YUI)发生冲突。
  注意,jQuery用"$"作为它自身的默认快捷方式。
  
  "$"的功能:
  当jQuery与其它库被加载后,你也可以不顾它的默认快捷方式而在任意一处通过调用jQuery.noConflict()函数来指定使用jQuery库,例如:

Java代码
  1. <html>   
  2. <head>   
  3.   <script src="prototype.js"></script>   
  4.   <script src="jquery.js"></script>   
  5.   <script>   
  6.     jQuery.noConflict();   
  7.        
  8.     // Use jQuery via jQuery(...)   
  9.     jQuery(document).ready(function(){   
  10.       jQuery("div").hide();   
  11.     });   
  12.        
  13.     // Use Prototype with $(...), etc.   
  14.     $('someid').style.display = 'none';   
  15.   </script>   
  16. </head>   
  17. <body></body>   
  18. </html>  
 <html>
 <head>
   <script src="prototype.js"></script>
   <script src="jquery.js"></script>
   <script>
     jQuery.noConflict();
     
     // Use jQuery via jQuery(...)
     jQuery(document).ready(function(){
       jQuery("div").hide();
     });
     
     // Use Prototype with $(...), etc.
     $('someid').style.display = 'none';
   </script>
 </head>
 <body></body>
 </html>


  这将使$回到它的原始库里,你依然可以在其它的应用程序里使用"jQuery"。
  另外,还有其它选项。如果你想确定jQuery不会与其它库冲突——但你又想自定义一个比较短快捷方式,你可以这么做:

Java代码
  1. <html>   
  2. <head>   
  3.   <script src="prototype.js"></script>   
  4.   <script src="jquery.js"></script>   
  5.   <script>   
  6.     var $j = jQuery.noConflict();   
  7.        
  8.     // Use jQuery via $j(...)   
  9.     $j(document).ready(function(){   
  10.       $j("div").hide();   
  11.     });   
  12.        
  13.     // Use Prototype with $(...), etc.   
  14.     $('someid').style.display = 'none';   
  15.   </script>   
  16. </head>   
  17. <body></body>   
  18. </html>  
 <html>
 <head>
   <script src="prototype.js"></script>
   <script src="jquery.js"></script>
   <script>
     var $j = jQuery.noConflict();
     
     // Use jQuery via $j(...)
     $j(document).ready(function(){
       $j("div").hide();
     });
     
     // Use Prototype with $(...), etc.
     $('someid').style.display = 'none';
   </script>
 </head>
 <body></body>
 </html>


  你能够自定义你自己的备用名称(如:jq、$J、awesomequery——可以是任何你想要的)。
  如果你不想给jQuery自定义其它备用名称(你更愿意使用$而不管其它库的$方法)而又不想与其它库相冲突时,以下的解决方法最常使用的。

Java代码
  1. <html>   
  2. <head>   
  3.   <script src="prototype.js"></script>   
  4.   <script src="jquery.js"></script>   
  5.   <script>   
  6.     jQuery.noConflict();   
  7.        
  8.     // Put all your code in your document ready area   
  9.     jQuery(document).ready(function($){   
  10.       // Do jQuery stuff using $   
  11.       $("div").hide();   
  12.     });   
  13.        
  14.     // Use Prototype with $(...), etc.   
  15.     $('someid').style.display = 'none';   
  16.   </script>   
  17. </head>   
  18. <body></body>   
  19. </html>  
 <html>
 <head>
   <script src="prototype.js"></script>
   <script src="jquery.js"></script>
   <script>
     jQuery.noConflict();
     
     // Put all your code in your document ready area
     jQuery(document).ready(function($){
       // Do jQuery stuff using $
       $("div").hide();
     });
     
     // Use Prototype with $(...), etc.
     $('someid').style.display = 'none';
   </script>
 </head>
 <body></body>
 </html>


  对于你的大部分代码而言,这或许是最理想的方式,因为你可以以改变最少的代码来实现完全的兼容性。

  参考:适合jQuery的快捷表示方式
  如果你不喜欢总是键入完整的"jQuery",有一些可供替换的快捷方式:
  重新指定jQuery为其它快捷方式

Java代码
  1. var $j = jQuery;  
var $j = jQuery;


  如果你想使用其它不同的库,这或许是最好的方法。
  使用下列方法,可以指定在一块代码内使用"$":

Java代码
function($) { // some code that uses $ })(jQuery)  
function($) { // some code that uses $ })(jQuery)


  备注:如果你使用了这种方法,在这块代码中你将不能使用"$"来调用Prototype方法,因为你选择了在这块代码中唯一使用jQuery方法。
  使用以下方法来实现DOM ready event:

Java代码
  1. jQuery(function($) { // some code that uses $ });  
jQuery(function($) { // some code that uses $ });


  备注:同样,在这个代码块里你也还有调用Prototype方法。

  原文档http://docs.jquery.com/Using_jQuery_with_Other_Libraries
  翻译有错漏的地方请多多指正,共同完善。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值