2011-07-13 35 views
3

我每次在我的網站上關注文本框時,都會使用jQuery執行操作。一切正常,只要我通過點擊它手動專注於文本框。使用jQuery檢測HTML5的自動對焦功能

但是:我使用的是HTML5的自動對焦功能,而且jQuery沒有檢測到文本框自動關注。我怎樣才能確保我的腳本執行操作,無論文本框是手動還是自動聚焦?

現在,我使用...

$("input:text, input:password").focus(function() { }); 

...當一個文本框被高亮檢測。

回答

6

如果你只需要擔心初始頁面加載時的自動對焦,那麼可能是這樣的?

觸發頁面加載上autofocus'd元素的焦點事件:

<script> 
$(document).ready(function() { 
    $("input:text, input:password").focus(function() { });//attach the focus event 

    $('input[autofocus]').trigger('focus');//force fire it on the autofocus element 
}); 
</script> 
+0

簡潔,美觀,而且完全解決了這個問題。謝謝,傑克! – wstr