2010-08-24 27 views

回答

5
$('#my_element_id').focus(); 

這是

$('#my_element_id').trigger('focus'); 

http://api.jquery.com/focus/

+1

但這並不選擇輸入字段的內容,不是嗎? – acme 2011-10-20 13:03:24

2
$('my_element_id').focus(); 
1

jQuerys focus()方法的快捷方式不選擇在輸入字段中的文本。取而代之的是,添加select()

$('my_element_id').focus().select(); 
4

原型的activate()函數集中在和選擇的形式的元素的全部內容。

jQuery中,這種行爲可以用三個功能被複制:

// Focus 
$('my_element_id').focus(); 

// Focus and select the content. 
$('my_element_id').focus().select(); 

// Focus and select the content without problems in Google chrome 
$('my_element_id').focus().select().mouseup(function(event){ 
    event.preventDefault(); 
}); 
相關問題