Javascript 加载时焦点输入框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4331022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Focus Input Box On Load
提问by Codex73
How can the cursor be focus on a specific input box on page load?
如何在页面加载时将光标集中在特定的输入框上?
Is it posible to retain initial text value as well and place cursor at end of input?
是否可以保留初始文本值并将光标放在输入末尾?
<input type="text" size="25" id="myinputbox" class="input-text" name="input2" value = "initial text" />
回答by jessegavin
There are two parts to your question.
你的问题有两个部分。
1) How to focus an input on page load?
1)如何将输入集中在页面加载上?
You can just add the autofocusattribute to the input.
您可以将autofocus属性添加到输入中。
<input id="myinputbox" type="text" autofocus>
However, this might not be supported in all browsers, so we can use javascript.
但是,这可能并非所有浏览器都支持,因此我们可以使用 javascript。
window.onload = function() {
var input = document.getElementById("myinputbox").focus();
}
2) How to place cursor at the end of the input text?
2) 如何将光标置于输入文本的末尾?
Here's a non-jQuery solution with some borrowed code from another SO answer.
这是一个非 jQuery 解决方案,其中有一些从另一个 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();
}
Here's an example of how I would accomplish this with jQuery.
这是我如何使用 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>
回答by David Calhoun
Just a heads up - you can now do this with HTML5 without JavaScript for browsers that support it:
提醒一下 - 您现在可以在支持 HTML5 的浏览器中使用 HTML5 执行此操作,而无需使用 JavaScript:
<input type="text" autofocus>
You probably want to start with this and build onto it with JavaScript to provide a fallback for older browsers.
您可能希望从这个开始,然后使用 JavaScript 构建它,以便为旧浏览器提供后备。
回答by Nasruddin
$(document).ready(function() {
$('#id').focus();
});
回答by crnlx
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">
回答by Camilo Díaz Repka
A portable way of doing this is using a custom function (to handle browser differences) like this one.
一种可移植的方法是使用像这样的自定义函数(处理浏览器差异)。
Then setup a handler for the onloadat the end of your <body>tag, as jessegavin wrote:
然后为标签onload末尾的设置一个处理程序<body>,正如 jessegavin 所写:
window.onload = function() {
document.getElementById("myinputbox").focus();
}
回答by Mohammed Javed
Working fine...
工作正常...
window.onload = function() {
var input = document.getElementById("myinputbox").focus();
}
回答by kavi
very simple one line solution:
非常简单的一行解决方案:
<body onLoad="document.getElementById('myinputbox').focus();">
回答by Marty
Add this to the top of your js
将此添加到您的js的顶部
var input = $('#myinputbox');
input.focus();
Or to html
或者到 html
<script>
var input = $('#myinputbox');
input.focus();
</script>
回答by KingRider
Try:
尝试:
Javascript Pure:
纯Javascript:
[elem][n].style.visibility='visible';
[elem][n].focus();
Jquery:
查询:
[elem].filter(':visible').focus();
回答by Roger
This is what works fine for me:
这对我来说很好用:
<form name="f" action="/search">
<input name="q" onfocus="fff=1" />
</form>
fff will be a global variable which name is absolutely irrelevant and which aim will be to stop the generic onload event to force focus in that input.
fff 将是一个全局变量,其名称绝对无关紧要,其目的是停止通用 onload 事件以强制关注该输入。
<body onload="if(!this.fff)document.f.q.focus();">
<!-- ... the rest of the page ... -->
</body>
From: http://webreflection.blogspot.com.br/2009/06/inputfocus-something-really-annoying.html
来自:http: //webreflection.blogspot.com.br/2009/06/inputfocus-something-really-annoying.html

