1. 程式人生 > >jquery dialog開啟時,焦點自動在第一個text框上

jquery dialog開啟時,焦點自動在第一個text框上

今天在用jqueryui的日曆功能,datepicker時碰到一個問題。輸入框在一個彈出的jquery dialog中,每次dialog(“open”)的時候,對話方塊的焦點自動focus到第一個<input type="text"/>框,正好這個input框綁定了datepicker,就自動把日曆彈出來了。

google了一下,還真碰到正好也有這個問題的:

http://stackoverflow.com/questions/1202079/prevent-jquery-ui-dialog-from-setting-focus-to-first-textbox

裡面有提到jquery對話方塊開啟時,哪個元素獲得焦點的機制:

http://jqueryui.com/upgrade-guide/1.10/#added-ability-to-specify-which-element-to-focus-on-open

在jqueryui 1.10.0之後,對話方塊開啟時,第一個設定了autofocus的元素獲得焦點。

在jqueryui 1.10.0之前:tabbable elements within the content area, tabbable elements within the button pane, the close button, and finally the dialog itself as a fallback。即:對話方塊主體內容中第一個支援tab鍵的元素;如果沒找到,則查詢button部分第一個支援tab鍵的元素;再沒找到,則close按鈕獲得焦點;最後沒有的話,整個對話方塊獲得焦點