2012年8月14日 星期二

jQuery 1.8 + jQuery UI的Dialog按鈕問題


看到jQuery 1.8 Release之後就高高興興地升級專案的jQuery,結果測試時發現Dialog的文字都不見了,
找了Google上相關的問題,發現似乎是一個Bug,
其解法也蠻簡單的,只要在使用dialog後接著修正的程式碼即可,
$('.ui-button-text').each(function ()
{
    var $this = $(this);
    if ($this.text() == '') //if it has no text (bugged) 
        $this.text($this.parent().attr('text')); //sets the text which was passed
});
這樣子,按鈕的文字就又回來了。


如果可以定義出一個只要使用dialog就會套用更新的方法的話,那就輕鬆多了,
這時就把主意打到了$.fn身上了,
這邊要注意的是,JavaScript本身是很好介入的程式語言,雖說可以很輕易地插入程式碼來包裝呼叫的函式,
卻要小心this的問題,倒也不用太過於在意,因為只要利用function.call就可以輕鬆解決。
(function ($)
{
    var dialog = $.fn.dialog;

    $.fn.dialog = function (opt)
    {
        dialog.call(this, opt);
        $('.ui-button-text').each(function ()
        {
            var $this = $(this);
            if ($this.text() == '') //if it has no text (bugged) 
                $this.text($this.parent().attr('text')); //sets the text which was passed
        });
    };
})(jQuery);
將這段程式碼插入到載入jQuery UI之後,就可以暫時地修正dialog的按鈕文字是空白的問題,這個解決方式,
倒是可以用來做為JavaScript程式擴充的參考。


原始文章出自於此

沒有留言:

張貼留言