---
title: "prototype.js 与 jquery.js冲突"
author: "john521"
date: "2014-07-16T23:37:21Z"
tags: []
categories: ["Web前端"]
slug: "prototype-js-jquery-js"
draft: false
lastmod: "2014-07-16T23:37:21Z"
id: 2092979
url: "https://john521.iteye.com/blog/2092979"
---

第一种情况：先加载Prototype，再加载jQuery  
方法一：jQuery 库和它的所有插件都是在jQuery名字空间内的，包括全局变量也是保存在jQuery 名字空间内的。

使用jQuery.noConflict();主要作用是在任何时候，只要在jQuery加载后就可以调用，将$符号的使用权返回给其它的js库，jQuery在创建它自己的名字空间时就将其它库的$保存在自己的一个变量当中。

**\[html]** [view plain](http://blog.csdn.net/aoyoo111/article/details/7364349 "view plain")[copy](http://blog.csdn.net/aoyoo111/article/details/7364349 "copy")[print](http://blog.csdn.net/aoyoo111/article/details/7364349 "print")[?](http://blog.csdn.net/aoyoo111/article/details/7364349 "?")

01. &lt;html&gt;
02. &lt;head&gt;
03. &lt;script src="prototype.js"&gt;&lt;/script&gt;
04. &lt;script src="jquery.js"&gt;&lt;/script&gt;
05. &lt;script type="text/javascript" &gt;
06. //各个js库之间的主要冲突在于$的冲突，这个方法是用来处理这个问题的
07. jQuery.noConflict();
08. //原本使用jQuery代码部分的$ 用jQuery替代
09. jQuery(document).ready(function (){
10. jQuery("div").hide();
11. });
12. // Use Prototype with $(...), etc.
13. $('proto').hide();
14. &lt;/script&gt;
15. &lt;/head&gt;
16. &lt;body&gt;&lt;/body&gt;
17. &lt;/html&gt;

方法二：如果你仍然想使用类似于$这样比较简短的字符，你可以将jQuery.noConflict()的返回值赋值给某个变量。这个变量就是jQuery的新缩写了，当然你可以使用$以外的任意字符串，比如：

**\[html]** [view plain](http://blog.csdn.net/aoyoo111/article/details/7364349 "view plain")[copy](http://blog.csdn.net/aoyoo111/article/details/7364349 "copy")[print](http://blog.csdn.net/aoyoo111/article/details/7364349 "print")[?](http://blog.csdn.net/aoyoo111/article/details/7364349 "?")

01. &lt;html&gt;
02. &lt;head&gt;
03. &lt;script src="prototype.js"&gt;&lt;/script&gt;
04. &lt;script src="jquery.js"&gt;&lt;/script&gt;
05. &lt;script type="text/javascript" &gt;
06. //$j就相当于jQuery，名称你可以自主定义
07. var  $j = jQuery.noConflict();
08. // Use jQuery via $j(...)
09. $j(document).ready(function (){
10. $j("div").hide();
11. });
12. // Use Prototype with $(...), etc.
13. $('proto').hide();
14. &lt;/script&gt;
15. &lt;/head&gt;
16. &lt;body&gt;&lt;/body&gt;
17. &lt;/html&gt;

方 法三：如果你还是想使用$，而不想使用别的字符，也是可以的。而且通常程序员都比较喜欢这样做，因为这样做写好的代码几乎都不用替换原来的$符号。那就是 利用名字空间的概念就所有的jQuery代码封装在document的ready事件名字空间范围内， 如：jQuery(document).ready(这里填入jQuery代码)

**\[html]** [view plain](http://blog.csdn.net/aoyoo111/article/details/7364349 "view plain")[copy](http://blog.csdn.net/aoyoo111/article/details/7364349 "copy")[print](http://blog.csdn.net/aoyoo111/article/details/7364349 "print")[?](http://blog.csdn.net/aoyoo111/article/details/7364349 "?")

01. &lt;html&gt;
02. &lt;head&gt;
03. &lt;script src="prototype.js"&gt;&lt;/script&gt;
04. &lt;script src="jquery.js"&gt;&lt;/script&gt;
05. &lt;script type="text/javascript" &gt;
06. jQuery.noConflict();
07. // Put all your code in your document ready area
08. jQuery(document).ready(function ($){
09. // 这样你可以在这个范围内随意使用$而不用担心冲突
10. $("div" ).hide();
11. });
12. // Use Prototype with $(...), etc.
13. $('proto' ).hide();
14. &lt;/script&gt;
15. &lt;/head&gt;
16. &lt;body&gt;&lt;/body&gt;
17. &lt;/html&gt;

 

第二种情况：先加载jQuery，再加载Prototype

按照这样的顺序加载，就不存在其它js库的$符号被jQuery占用的问题。所以对其它的js库的代码可以不作任何修改，照常使用$，而对 jQuery可以使用jQuery来替代$.如：

**\[html]** [view plain](http://blog.csdn.net/aoyoo111/article/details/7364349 "view plain")[copy](http://blog.csdn.net/aoyoo111/article/details/7364349 "copy")[print](http://blog.csdn.net/aoyoo111/article/details/7364349 "print")[?](http://blog.csdn.net/aoyoo111/article/details/7364349 "?")

01. &lt;html&gt;
02. &lt;head&gt;
03. &lt;script src="jquery.js"&gt;&lt;/script&gt;
04. &lt;script src="prototype.js"&gt;&lt;/script&gt;
05. &lt;script type="text/javascript" &gt;
06. // 使用 jQuery 用 jQuery(...)
07. jQuery(document).ready(function (){
08. jQuery("div" ).hide();
09. });
10. // 使用 Prototype 时，用 $(...),
11. $('someid' ).hide();
12. &lt;/script&gt;
13. &lt;/head&gt;
14. &lt;body&gt;&lt;/body&gt;
15. &lt;/html&gt;

或者你不想写jQuery这么长的字符，你可以通过另外一种方法：

**\[html]** [view plain](http://blog.csdn.net/aoyoo111/article/details/7364349 "view plain")[copy](http://blog.csdn.net/aoyoo111/article/details/7364349 "copy")[print](http://blog.csdn.net/aoyoo111/article/details/7364349 "print")[?](http://blog.csdn.net/aoyoo111/article/details/7364349 "?")

1. var  $j = jQuery;