【发布时间】:2010-09-17 16:01:29
【问题描述】:
页面加载后,我想将光标移动到特定字段。没问题。但我还需要选择并突出显示该文本字段中的默认值。
【问题讨论】:
标签: javascript html
页面加载后,我想将光标移动到特定字段。没问题。但我还需要选择并突出显示该文本字段中的默认值。
【问题讨论】:
标签: javascript html
来自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!" />
【讨论】:
在您的输入标签中,放置以下内容:
onFocus="this.select()"
【讨论】:
Version 55.0.2883.95 (64-bit))
试试这个。这适用于 Firefox 和 chrome。
<input type="text" value="test" autofocus="autofocus" onfocus="this.select()">
【讨论】:
在页面加载时执行:
window.onload = function () {
var input = document.getElementById('myTextInput');
input.focus();
input.select();
}
<input id="myTextInput" value="Hello world!" />
【讨论】:
我找到了一个非常简单的方法,效果很好:
<input type="text" onclick="this.focus();this.select()">
【讨论】:
使用 jquery 时...
html:
<input type='text' value='hello world' id='hello-world-input'>
jquery:
$(function() {
$('#hello-world-input').focus().select();
});
【讨论】:
focus(),直接调用select()。
var input = document.getElementById('myTextInput');
input.focus();
input.setSelectionRange( 6, 19 );
<input id="myTextInput" value="Hello default value world!" />
在文本字段中选择特定文本
你也可以用like
input.selectionStart = 6;
input.selectionEnd = 19;
【讨论】:
使用autofocus 属性可以很好地用于文本输入和复选框。
<input type="text" name="foo" value="boo" autofocus="autofocus"> FooBoo
<input type="checkbox" name="foo" value="boo" autofocus="autofocus"> FooBoo
【讨论】:
让输入文本字段在页面加载时自动获得焦点:
<form action="/action_page.php">
<input type="text" id="fname" name="fname" autofocus>
<input type="submit">
</form>
【讨论】:
在你的输入标签中使用这样的自动对焦
<input type="text" autofocus>
【讨论】: