2008-10-17 16 views

回答

81

http://www.codeave.com/javascript/code.asp?u_log=7004

var input = document.getElementById('myTextInput'); 
 
input.focus(); 
 
input.select();
<input id="myTextInput" value="Hello world!" />

+0

這適用於Firefox,但不會選擇Chrome或Edge中的文本。 – Vincent 2015-09-09 23:19:52

+0

對焦法似乎工作正常 – edward 2016-04-22 07:42:59

14

要做到這一點在頁面加載:

27

在你輸入標籤,放置以下:

onFocus="this.select()" 
+0

這隻適用於FireFox。在Chrome中,您可以通過添加「onmouseup ='return false'」來解決此問題,但這不會解決Edge中的問題,如果您使用鍵盤導航到元素,也不能解決問題。 – Vincent 2015-09-09 23:17:48

-1
<input type="text" value="test" onclick="this.select()"> 

測試試試這個。這將適用於Firefox和Chrome。

<input type="text" value="test" autofocus="autofocus" onfocus="this.select()">

14

<input type="text" onclick="this.focus();this.select()"> 
0

使用jQuery的時候...

HTML:

<input type='text' value='hello world' id='hello-world-input'> 

的jQuery:

$(function() { 
    $('#hello-world-input').focus().select(); 
}); 

例如:https://jsfiddle.net/seanmcmills/xmh4e0d4/

相關問題