2013-06-01 10 views
0

HTML:Focus在輸入字段,在其他事件

<form> 
    <input id="inp" type="text" value="asd" /> 
</form> 

<div id="button"> 
    button 
</div> 

JS:

$("#button").on("click", function() { 
    $("#inp").focus(); 
}); 

DEMO: http://jsfiddle.net/YgZx8/1/

如果點擊button,上面的代碼只是selects(而不是發生focusing)輸入字段中的文本。這發生在safari和chrome中,正確地在opera和firefox中運行。

問題:如何在chrome和safari中輸入focusing

+0

你的意思是你要關注它_without_選擇它還是什麼? –

+0

@ BenjaminGruenbaum - 是的,我需要那只是將鼠標指針集中到輸入,沒有文字選擇 – RIKI

+0

@Zenith - 我們可以有不同的瀏覽器版本... – RIKI

回答

3

也許試試這個:

http://jsfiddle.net/YgZx8/10/

$("#button").on("click", function() { 
    $('#inp').focus().val($('#inp').val()); 
}); 
+0

謝謝,這是需要的 – RIKI

1
$("#button").on("click", function() { 
    $("#inp")[0].selectionStart = $("#inp")[0].selectionEnd = $('#inp').val().length; 
}); 
相關問題