相关疑难解决方法(0)

在页面加载时将焦点设置在HTML输入框上

我正在尝试在页面加载时将默认焦点设置在输入框上(例如:google).我的页面非常简单,但我无法弄清楚如何做到这一点.

这是我到目前为止所得到的:

<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>
Run Code Online (Sandbox Code Playgroud)

为什么这种方法不能正常工作呢?

<html>
<head>
<script type="text/javascript">
function FocusOnInput()
{ …
Run Code Online (Sandbox Code Playgroud)

html javascript focus onload autofocus

95
推荐指数
3
解决办法
19万
查看次数

标签 统计

autofocus ×1

focus ×1

html ×1

javascript ×1

onload ×1