我試圖在頁面加載時設置輸入框的默認焦點(例如: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