js 引入juery.js 和Prototype.js衝突解決($,$F)

來源:互聯網
上載者:User

標籤:io   使用   java   ar   sp   cti   on   c   代碼   

 

在頁面中同時存在jquery 和 prototype ,當用到 $ 的時候,難免產生衝突,所以一定要區分開來:

<script type="text/javascript" src="jquery.js"></script> 
<script type="text/javascript"> 
    j= $; 
 </script> 
<script type="text/javascript" src="prototype.js" ></script>

這樣做了以後,在用到jquery 的地方用 j 來替代 $ 符號,而 prototype 的 $ 函數照常使用,當然其實也不用定義“ j= $; ” ,在用到 jquery 的地方直接用 jQuery 來替代 $ 符號也一樣的。

 

$("") 改成jQuery("");  $ 改成 j

我的js包引用方法:

head標籤裡添加jquery.js, prototype .js放到底部去應用,還有需要強調的一點就是j=$這個js代碼jquery.js盡量放在所有js包的最上面,因為後面的js包可能會引用到jquery包,如果不這樣做的,會導致後面的js包屬性和方法不支援

 

還有一個衝突網上找到的:Array.prototype.push 方法衝突。轉載下:

這個衝突很嚴重,直接導致jQuery 的級聯篩選無效。比如正常情況下,你可以使 用 $("span",$("#main")) 來擷取 ID為 main 的元素下面的 span 元素。但如果頁面中同時使用了 prototype 架構相關,那抱歉 的很,你永遠擷取不到了。原因你可以調試跟蹤自己查看 jQuery 的原始碼。 
解決方案: 
    <script type="text/javascript"> 
        var fnArrayPush = Array.prototype.push; 
        $().ready(function(){ 
            Array.prototype.push = fnArrayPush; 
             init(); 
        });     
     </script>

 

 

 

 

js 引入juery.js 和Prototype.js衝突解決($,$F)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.