Bookmark and Share

Lee's 程序人生

HTML CSS Javascript XML AJAX ATLAS C# C++ 数据结构 软件工程 设计模式 asp.net Java 数字图象处理 Sql 数据库
  博客园  :: 首页  :: 新随笔  :: 联系 :: 管理

jQuery 结合其他库一起使

Posted on 2007-10-19 16:45  analyzer  阅读(449)  评论(1)    收藏  举报
jQuery 结合其他库一起使用,为了解决名字空间的冲突,以下是三种解决办法:

第一种方法是直接调用 jQuery.noConflict():

<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>

第二种方法是赋值给一个简短的名字:

<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>

第三种方法,通过传递$给ready函数参数作为参数,就可以在ready的参数funcion中使

用$:

<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>