除了Ajax()翻新及引進Deferred物件的大革新,jQuery 1.5還有些新東西:

$.sub()
$.sub()可產生jQuery函數的分身,允許我們修改產生客製版的jQuery於特殊範圍內使用,不影響程式的其他部分引用jQuery。如以下的例子,我客製了一個特別版jQuery--$$,會在click註冊事件時,偷偷插入一個alert(“Hacking”),如此可在不影響其他jQuery功能的情況下,有限度修改jQuery的行為。這在寫plugin時格外有用,細節可參考jQuery UI的範例

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.min.js" 
        type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $$ = $.sub();
            $$.fn.click = function (fn) {
                return this.each(function () {
                    $(this).click(function () {
                        alert("Hacking");
                    }).click(fn);
                });
            };
            $$("#b1").click(function () {
                alert("b1 cicked");
            });
            $("#b2").click(function () {
                alert("b2 clicked");
            });
        });
    </script>
</head>
<body>
<input type="button" value="Injected" id="b1" />
<input type="button" value="Normal" id="b2" />
</body>

效能改進
依循每次改版都會調效能的慣例,1.5版改善了.children(), .prev(), .next()等方法的執行效率,相關數據可參考官方公告

開發環境調整
jQuery團隊使用的開發建置系統由Java/Rhino改為Javascript/NodeJS,擺脫了對Java的依賴;而製作濃縮版本jQuery-1.5.min.js的工具也由Google Closure Compiler改為UglifyJS,主要著眼於追求檔案尺寸的再濃縮。

說好的jQuery Templates與Data Link Plugin呢?
原本預期jQuery 1.5會將Templates及Data Link兩個官方Plugin納入核心,不過恐怕要讓人失望了,依John Resig的說法:

Those are just official jQuery plugins – they are not included in the jQuery 1.5 release. The major features of this release include a complete rewrite of the Ajax module, deferreds, and subclassing.


Comments

# by 鐵衛

不把 plugin 納進去也好,感覺不見得每個人都會用到這兩個 plugin 卻每次要載入,這樣反而造成時間和頻寬的浪費,所以其實分開也沒什麼不好的 ...

# by KenChao

可否請問一下,我想要閱讀jquery產生出來的html碼,是否有方便的工具,因為我的jquery程式碼太長了很難設定中斷點去看,所以想要瀏覽整個執行完的html碼,但是我使用瀏覽器的檢視原始碼後,都只能看到jquery的程式碼,無法看到瀏覽器真正得到的html,請問是否有辦法閱讀瀏覽器真正收到的html呢?..

# by Jeffrey

to KenChao, 你是指去看jQuery.append(...)等動作動態加入元素後的HTML嗎? IE裡有IE Dev Tools, FireFox裡有FireBug等工具,都有用滑鼠去點選網頁上看得見的元素,去找出其背後的HTML、CSS等資訊。http://msdn.microsoft.com/en-us/library/dd565627(v=vs.85).aspx 的Selecting Objects on a Web Page一節有使用IE Dev Tools檢視HTML元素的操作介紹。

Post a comment