整合jQuery和Prototype遇到的問題.

來源:互聯網
上載者:User

標籤:style   blog   color   使用   檔案   問題   

由於項目要在舊的伺服器上面運行,而舊的伺服器底層用了Prototype,所以需要解決jQuery和Prototype衝突的問題。

一.$符號衝突問題

這個還是很好解決的。

jQuery.noConflict()

而如果不想將原來所有$符號換成jQuery,可以用即時函數包住原來的jQuery代碼:

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

 

二. for in 數組迴圈問題

for in 語句本身並不是一個 "bad practice",但是卻容易被誤用,比如用在數組上。

使用for in語句首先要注意,如果不能百分百確定,那麼請使用hasOwnProperty:

for (var prop in obj) {   if (obj.hasOwnProperty(prop)) {       // prop is not inherited   }}

 

 

其次,不要用for in對數組或類數組對象(array-like)。由於Prototype會自動為所有數組添加一推屬性,即使用上面hasOwnProperty的方式也無法過濾。不過可以用下面這種方式:

for (var prop in obj) {    if (Object.prototype.hasOwnProperty.call(obj, prop)) {    // prop is not inherited    }}

 

 

三. IE彈框問題 “Stop running this script”

這是一個很奇怪的問題。原來的架構在IE沒有問題,但是匯入Prototype之後,IE彈框

Stop running this script?A script on this page is causing your web browser to run slowly. If it continues to run, your computer might become unresponsive.

 

對於這個問題完全摸不著頭腦,通過刪除引入檔案終於找到罪魁禍首,一個兩萬多行的檔案。一開始還以為裡面某個語句導致了這個問題,但是發現並不是。將檔案分成兩個檔案後,就沒有出現這個彈框了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.