我试图在页面加载时设置输入框的默认焦点(例如:google)。 我的网页很简单,但我无法弄清楚如何做到这一点。在页面加载时将焦点设置为HTML输入框
这是我到目前为止有:
<html>
<head>
<title>Password Protected Page</title>
<script type="text/javascript">
function FocusOnInput()
{
document.getElementById("PasswordInput").focus();
}
</script>
<style type="text/css" media="screen">
body, html {height: 100%; padding: 0px; margin: 0px;}
#outer {width: 100%; height: 100%; overflow: visible; padding: 0px; margin: 0px;}
#middle {vertical-align: middle}
#centered {width: 280px; margin-left: auto; margin-right: auto; text-align:center;}
</style>
</head>
<body onload="FocusOnInput()">
<table id="outer" cellpadding="0" cellspacing="0">
<tr><td id="middle">
<div id="centered">
<form action="verify.php" method="post">
<input type="password" name="PasswordInput"/>
</form>
</div>
</td></tr>
</table>
</body>
</html>
怎么会不工作,而这个工作正常?
<html>
<head>
<script type="text/javascript">
function FocusOnInput()
{
document.getElementById("InputID").focus();
}
</script>
</head>
<body onload="FocusOnInput()">
<form>
<input type="text" id="InputID">
</form>
</body>
</html>
帮助深表感谢:-)
“适用于所有的主流浏览器” - 即真的取决于您对“所有当前浏览器”的定义。 – 2010-08-01 20:30:52
ie9:http://stackoverflow.com/questions/8280988/how-to-make-input-autofocus-in-internet-explorer – jedierikb 2013-06-11 14:06:35
@BhaveshGangani ['autofocus' is不支持Internet Explorer 9和更早版本。](http://www.w3schools.com/tags/att_input_autofocus.asp) – 2014-09-09 13:58:40