【发布时间】:2011-05-18 21:03:40
【问题描述】:
如何在页面加载时将光标聚焦在特定的输入框上?
是否也可以保留初始文本值并将光标置于输入末尾?
<input type="text" size="25" id="myinputbox" class="input-text" name="input2" value = "initial text" />
【问题讨论】:
标签: javascript html dom xhtml
如何在页面加载时将光标聚焦在特定的输入框上?
是否也可以保留初始文本值并将光标置于输入末尾?
<input type="text" size="25" id="myinputbox" class="input-text" name="input2" value = "initial text" />
【问题讨论】:
标签: javascript html dom xhtml
您的问题分为两部分。
1) 如何将输入集中在页面加载上?
您只需将autofocus 属性添加到输入。
<input id="myinputbox" type="text" autofocus>
但是,这可能不是所有浏览器都支持,所以我们可以使用 javascript。
window.onload = function() {
var input = document.getElementById("myinputbox").focus();
}
2) 如何将光标置于输入文本的末尾?
这是一个非 jQuery 解决方案,其中有一些来自 another SO answer 的借用代码。
function placeCursorAtEnd() {
if (this.setSelectionRange) {
// Double the length because Opera is inconsistent about
// whether a carriage return is one character or two.
var len = this.value.length * 2;
this.setSelectionRange(len, len);
} else {
// This might work for browsers without setSelectionRange support.
this.value = this.value;
}
if (this.nodeName === "TEXTAREA") {
// This will scroll a textarea to the bottom if needed
this.scrollTop = 999999;
}
};
window.onload = function() {
var input = document.getElementById("myinputbox");
if (obj.addEventListener) {
obj.addEventListener("focus", placeCursorAtEnd, false);
} else if (obj.attachEvent) {
obj.attachEvent('onfocus', placeCursorAtEnd);
}
input.focus();
}
这是我如何使用 jQuery 完成此任务的示例。
<input type="text" autofocus>
<script>
$(function() {
$("[autofocus]").on("focus", function() {
if (this.setSelectionRange) {
var len = this.value.length * 2;
this.setSelectionRange(len, len);
} else {
this.value = this.value;
}
this.scrollTop = 999999;
}).focus();
});
</script>
【讨论】:
function focusOnMyInputBox(){
document.getElementById("myinputbox").focus();
}
<body onLoad="focusOnMyInputBox();">
<input type="text" size="25" id="myinputbox" class="input-text" name="input2" onfocus="this.value = this.value;" value = "initial text">
【讨论】:
一种可移植的方式是使用自定义函数(处理浏览器差异),例如this one。
然后在 <body> 标签的末尾为 onload 设置一个处理程序,正如 jessegavin 所写:
window.onload = function() {
document.getElementById("myinputbox").focus();
}
【讨论】:
请注意 - 您现在可以在支持 HTML5 的浏览器中使用不带 JavaScript 的 HTML5:
<input type="text" autofocus>
您可能希望从这里开始,然后使用 JavaScript 构建它,以便为旧版浏览器提供后备。
【讨论】:
$(document).ready(function() {
$('#id').focus();
});
【讨论】:
这对我来说很好用:
<form name="f" action="/search">
<input name="q" onfocus="fff=1" />
</form>
fff 将是一个全局变量,其名称绝对无关紧要,其目的是停止通用 onload 事件以强制关注该输入。
<body onload="if(!this.fff)document.f.q.focus();">
<!-- ... the rest of the page ... -->
</body>
发件人:http://webreflection.blogspot.com.br/2009/06/inputfocus-something-really-annoying.html
【讨论】:
如果由于某种原因无法添加到 BODY 标记中,可以在表单之后添加:
<SCRIPT type="text/javascript">
document.yourFormName.yourFieldName.focus();
</SCRIPT>
【讨论】:
工作正常...
window.onload = function() {
var input = document.getElementById("myinputbox").focus();
}
【讨论】:
试试:
纯Javascript:
[elem][n].style.visibility='visible';
[elem][n].focus();
Jquery:
[elem].filter(':visible').focus();
【讨论】:
非常简单的一行解决方案:
<body onLoad="document.getElementById('myinputbox').focus();">
【讨论】:
将此添加到您的 js 的顶部
var input = $('#myinputbox');
input.focus();
或者到html
<script>
var input = $('#myinputbox');
input.focus();
</script>
【讨论】: