jquery版本冲突,在同一个页面使用多个不同的jQuery版本,让它们并存而不冲突

jQuery自诞生以来,版本越来越多,而且jQuery官网的新版本还在不断的更新和发布中,现已经达到了1.6.4版本,但是我们在以前的项目中就已经使用了旧版本的jQuery,比如已经出现的:1.3.X、1.4.X、1.5.X、1.6.2等等。

  由于项目的需要,必然也需要不断的使用较新版的jQuery,但对于原来就已经存在并已经采用了的旧jQuery版本,我们如何让多个不同的jQuery版本在同一个页面并存而不冲突呢?

  其实,利用jQuery.noConflict()特性,我们不仅可以让jQuery与其他的JS库并存,比如Prototype。也可以与jQuery本身的其他不同版本并存而不冲突。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 <html>
     <head>
         <title>在同一个页面中加载多个不同的jQuery版本</title>
         <!-- 从谷歌服务器加载jQuery最新版本 -->
         <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
         <script type="text/javascript">
             var jQuery_New = $.noConflict(true);
         </script>
         <!-- 加载jQuery1.6.2版本 -->
         <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
         <script type="text/javascript">
             var jQuery_1_6_2 = $.noConflict(true);
         </script>
         <!-- 加载jQuery1.5.2版本 -->
         <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
         <script type="text/javascript">
             var jQuery_1_5_2 = $.noConflict(true);
         </script>
         <!-- 加载jQuery1.4.2版本 -->
         <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
         <script type="text/javascript">
             var jQuery_1_4_2 = $.noConflict(true);
         </script>
         <!-- 加载jQuery1.3.2版本 -->
         <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
         <script type="text/javascript">
             var jQuery_1_3_2 = $.noConflict(true);
         </script>
         <script type="text/javascript">
             alert(jQuery_New.fn.jquery);
             alert(jQuery_1_6_2.fn.jquery);
             alert(jQuery_1_5_2.fn.jquery);
             alert(jQuery_1_4_2.fn.jquery);
             alert(jQuery_1_3_2.fn.jquery);
             
             jQuery_New(function($){$('<p>我是最新的'+$.fn.jquery+'版本添加进来的。</p>').appendTo('body');});
             jQuery_1_6_2(function($){$('<p>我是'+$.fn.jquery+'版本添加进来的。</p>').appendTo('body');});
             jQuery_1_5_2(function($){$('<p>我是'+$.fn.jquery+'版本添加进来的。</p>').appendTo('body');});
             jQuery_1_4_2(function($){$('<p>我是'+$.fn.jquery+'版本添加进来的。</p>').appendTo('body');});
             jQuery_1_3_2(function($){$('<p>我是'+$.fn.jquery+'版本添加进来的。</p>').appendTo('body');});
         </script>
     </head>
     <body>
         在同一个页面中加载多个不同的jQuery版本
         <br>
     </body>
 </html>

  转载:http://www.cnblogs.com/liyunqi007/archive/2011/10/22/2221178.html

 

备注:js文件中“$****”改为

jQuery_New(function($){
  $(document).ready(function () {
       
    });
})

jQuery_New(function($){
  $('link[@rel*=style][@title]').each(function (i) {
      *****
    });
})

  

posted @ 2013-06-05 22:07  酉乐  阅读(200)  评论(0编辑  收藏  举报