簡介:
本文檔是一次調研總結,直接貼過來的,沒有作背景鋪墊修改。這是一個關於jQuery1.9較之前版本變化的調研,完了之后,總結了這么一個對於“jQuery 1.9移除方法的替代總結”的文檔,不一定是最優的解決方案,但我所提到的方法絕對可行。
本文檔總結了一些jQuery 1.9較之前版本的變化(原文:http://jquery.com/upgrade-guide/1.9/),看官方Blog,JQuery2.0已經出到了Beta2 Released版本,並且不再支持IE 6/7/8.
以下為涉及到Gaia 1.0的幾個變化:
jQuery.browser()
jQuery.browser() removedThe |
這里給出的是用Modernizr(一個利用JS和CSS來檢測瀏覽器所支持功能的小工具)來檢測瀏覽器所支持功能,其實官網還給出了另一種解決方案:
Because |
Gaia1.0中用到JQuery.browser()方法的為:
$.browser.msie && $.browser.version <= 8
作為常用的兩種方法,
JQuery.browser.mise(如果是IE則返回true)可以用JQuery.support.boxModel(如果IE瀏覽器是QuirksMode方式運行,則返回false)代替;
jQuery.browser.version <= 8可以用jQuery. support.leadingWhitespace(判斷瀏覽器是否為IE 6~8版本)代替;
這樣上述語句可以改為:
$.support.boxModel && $.support.leadingWhitespace
另外,jQuery.support.objectAll可判斷瀏覽器是否為IE 7~8版本。由於jQuer2.0不再支持IE9之前的版本,日后升級還需根據官方推薦判斷瀏覽器類型及版本加載不同的jQuery。如官方推薦方式;
<!--[if lt IE 9]> <script src='jquery-1.9.0.js'></script> <![endif]--> <!--[if gte IE 9]> <script src='jquery-2.0.0.js'></script> <![endif]--> |
如果必須要繼續使用jQuery.browser()可以添加“jquery-browser”插件,但我沒有測試該插件。
.live()
link .live() removed The .live() method has been deprecated since jQuery 1.7 and has been removed in 1.9. We recommend upgrading code to use the .on() method instead. To exactly match $("a.foo").live("click", fn), for example, you can write $(document).on("click", "a.foo", fn). For more information, see the .on() documentation. In the meantime, the jQuery Migrate plugin can be used to restore the .live() functionality. |
.live()方法在1.9中移除,@ZPS在郵件中已經告知過大家。對於.live()方法的移除,升級比較簡單,僅僅是將“.live()”替換為“.on()”。
.die()
.die() removed The .die() method has been deprecated since jQuery 1.7 and has been removed in 1.9. We recommend upgrading code to use the .off() method instead. To exactly match $("a.foo").die("click"), for example, you can write $(document).off("click", "a.foo"). For more information, see the .off() documentation. In the meantime, the jQuery Migrate plugin can be used to restore the .die() functionality. |
相對於“.live()”方法的移除,“.die()”方法也從1.9中移除,取而代之的是“.off()”方法。正如在1.9之前,很多人只關注過“.live()”方法,卻不知道還有個“.die()”方法,或許還會有Coder不知道如何去掉.on()添加的事件,其實就是用“.off()”進行刪除添加的事件。
jQuery.sub()
jQuery.sub() removed The jQuery.sub() method has been moved to the jQuery Migrate plugin. The number of use cases where it proved valuable were not enough to justify keeping it in core. The jQuery Migrate plugin adds back this functionality. |
.sub()方法可以創建一個新的jQuery副本而不影響原有的jQuery對象,我對該方法的理解是:其實.sub()方法就是增加或重寫jQuery的方法或創建新plugin,有待討論。
從上面升級指南上來看,.sub()方法並沒有被removed,而是被moved到其他plugin,所以應該是還可以用的,只要引用相應的plugin。
官方給出的使用.sub()的兩個特定情況:一是在不改變原有方法的前提下提供一種簡單的重寫jQuery方法的途徑,二是幫助用戶解決jQuery plugin封裝和基本命名空間。翻譯晦澀,大家請看原文:
There are two specific use cases for which jQuery.sub() was created. The first was for providing a painless way of overriding jQuery methods without completely destroying the original methods and another was for helping to do encapsulation and basic namespacing for jQuery plugins.
.toggle(function, function, … )
link .toggle(function, function, ... ) removed This is the "click an element to run the specified functions" signature of .toggle(). It should not be confused with the "change the visibility of an element" of .toggle() which is not deprecated. The former is being removed to reduce confusion and improve the potential for modularity in the library. The jQuery Migrate plugin can be used to restore the functionality. |
需要注意的是該.toggle()是“綁定兩個或多個處理程序,在點擊時循環執行”;另一個.toggle()仍然存在,它是“控制相應組件的顯示和隱藏”;中文晦澀,官方對此二方法的說明如下:
Categories: Deprecated > Deprecated 1.8 | Events > Mouse Events .toggle(handler(eventObject), handler(eventObject) [,handler(eventObject)])Returns:jQueryversion deprecated: 1.8, removed: 1.9Description:Bind two or more handlers to the matched elements, to be executed on alternate clicks. |
.toggle( [duration ] [, complete ] )Returns:jQueryDescription:Display or hide the matched elements. |
這個變化值得注意。對於刪除的這個“.toggle()”方法,官方沒有給出升級措施,但我發現一個方法名和描述都比較相似的方法“.trigger()”,不知道可不可以替代,還請大家賜教。
另外,國外有論壇提到“jQuery Migrate Plugin”插件,可以使用該插件檢測在jQuery 1.9 或2.0中哪些功能已經啟用或移除。我還沒有學習到,大家參看項目README吧。(谷歌這個插件,全是E文;百度這個插件,過半是E文)
頁面凌亂,希望能幫到大家,謝謝
Lionden 2013年3月20日
E-Mail:hsdlionden@gmail.com
轉載請注明博客園:http://www.cnblogs.com/lionden/