I have a very simple one page website that has a input field in it. I want to make the input field focused when the window/site is loaded so that the user/visitor can start typing on it right away the site is loaded!
我有一个非常简单的单页网站,里面有一个输入字段。我想在加载窗口/站点时使输入字段成为焦点,以便用户/访问者可以立即开始在网站上键入内容!
I want it NOT IN JQUERY. IF POSSIBLE PLEASE GIVE ME A SOLUTION IN JAVASCRIPT PLEASE.
我想要它不是在观察。如果可能请给我JAVASCRIPT的解决方案请。
Thanks in advance.
提前致谢。
7 个解决方案
#1
1
There is a new input field attribute call autofocus
. Supported by all browsers except IE http://www.html5tutorial.info/html5-autofocus.php
有一个新的输入字段属性调用自动对焦。除了IE http://www.html5tutorial.info/html5-autofocus.php之外的所有浏览器都支持
You can use that and provide fallback with the solutions others are providing
您可以使用它并使用其他人提供的解决方案提供后备
var i = document.createElement('input');
if(!('autofocus' in i)) {
window.onload=function(){
document.getElementById('ID').focus();
});
}
#2
4
window.onload=function(){
document.getElementById('ID').focus();
});
#3
2
//ID is the id of textbox
// ID是文本框的id
document.getElementById('ID').focus();
#4
1
document.getElementById("ControlID").focus();
#5
1
Add it to the onLoad callback so that it will focus after the page has been loaded:
将它添加到onLoad回调中,以便在页面加载后它将聚焦:
<body onLoad="function() { document.getElementById('input_field_id').focus(); }">
#6
0
You need to use focus()
method in javascript.
你需要在javascript中使用focus()方法。
In body onload method, call the javascript method, where you need to set the focus on the input field.
在body onload方法中,调用javascript方法,您需要在输入字段上设置焦点。
<script type="text/javascript">
function load()
{
document.getElementById("inputFieldId").focus();
}
</script>
<body onload="load()">
#7
0
HTML5 has autofocus attribute for this job. You could check from here if your target browser(s) has support for this attribute.
HTML5具有此作业的自动聚焦属性。您可以从此处查看目标浏览器是否支持此属性。
#1
1
There is a new input field attribute call autofocus
. Supported by all browsers except IE http://www.html5tutorial.info/html5-autofocus.php
有一个新的输入字段属性调用自动对焦。除了IE http://www.html5tutorial.info/html5-autofocus.php之外的所有浏览器都支持
You can use that and provide fallback with the solutions others are providing
您可以使用它并使用其他人提供的解决方案提供后备
var i = document.createElement('input');
if(!('autofocus' in i)) {
window.onload=function(){
document.getElementById('ID').focus();
});
}
#2
4
window.onload=function(){
document.getElementById('ID').focus();
});
#3
2
//ID is the id of textbox
// ID是文本框的id
document.getElementById('ID').focus();
#4
1
document.getElementById("ControlID").focus();
#5
1
Add it to the onLoad callback so that it will focus after the page has been loaded:
将它添加到onLoad回调中,以便在页面加载后它将聚焦:
<body onLoad="function() { document.getElementById('input_field_id').focus(); }">
#6
0
You need to use focus()
method in javascript.
你需要在javascript中使用focus()方法。
In body onload method, call the javascript method, where you need to set the focus on the input field.
在body onload方法中,调用javascript方法,您需要在输入字段上设置焦点。
<script type="text/javascript">
function load()
{
document.getElementById("inputFieldId").focus();
}
</script>
<body onload="load()">
#7
0
HTML5 has autofocus attribute for this job. You could check from here if your target browser(s) has support for this attribute.
HTML5具有此作业的自动聚焦属性。您可以从此处查看目标浏览器是否支持此属性。