2012-08-30 22 views
0

我試圖儘快更改一個按鈕的圖標,只要它被使用。 的代碼非常簡單:爲什麼更改jQuery UI Button的圖標會在Internet Explorer中中斷我的鏈接?

<a id='just-a-link' target="_blank" href="http://google.com">Google</a> 

jQuery('#just-a-link').button().bind('click', function() { 

    var icons = jQuery(this).button("option", "icons"); 
    icons.primary = 'ui-icon-plus'; 

    jQuery(this).button("option", "icons", icons); 

}); 

但是Internet Explorer中的鏈接不會被解僱。 所以點擊的鏈接會改變圖標,但不會打開頁面

你有什麼建議來解決這個問題?

http://jsfiddle.net/YnwnU/

在撥弄你可以看到jQuery(this).button("option", "icons", icons);導致的錯誤作爲第二個按鈕的功能打開鏈接。

+0

它可能值得注意的是,這在其他瀏覽器,它看起來像一個IE特定的錯誤。 – eis

回答

2

好好看了一下後,它看起來像是因爲你已經建立的.button()調用中調用.button(),它恰好是殺死-href-或任何事件處理程序已經設置它。 (在這種情況下打開一個新窗口&鏈接。

jQuery('#just-a-link').button().bind('click', function() { 
    var icons = jQuery(this).button("option", "icons"); 
    icons.primary = 'ui-icon-plus'; 

    // Adding this makes it work in IE for at least the code you currently have 
    window.open($(this).attr('href')); 
}); 

它看起來更加像你對我試圖只是一個圖標添加到所有外部鏈接,並在新窗口中打開。爲什麼不只是做呢?

var icons = jQuery(this).button("option", "icons"); 
    icons.primary = 'ui-icon-plus'; 

jQuery('a[target="_blank"]').button().button("option", "icons", icons); 

無需綁定點擊......只是target="_blank"設置所有錨按鈕&給他們的圖標。這也使得它在IE和其他瀏覽器自動工作。搏一搏。

updated jsFiddle Demo

+0

這並沒有真正回答爲什麼它需要爲IE瀏覽器,但不是爲其他人......但它當然對那個「該怎麼辦」:)的問題:) – eis

+0

更新!認爲這只是因爲.button()再次被調用 –

+0

@mcpDESIGNS一些用戶的連接速度很慢,加載應用程序需要幾秒鐘的時間。所以他們在按鈕上多次點擊。爲了防止這種情況,我們禁用按鈕並添加一個微調圖像;) - 'target = _blank'只是更容易調試。 – jantimon

相關問題