我试图在页面加载时将默认焦点设置在输入框上(例如: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>

这个不错的时候怎么会不管用呢?

<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>

非常感谢您的帮助:-)

推荐答案

这一行:

<input type="password" name="PasswordInput"/>

应具有id属性,如下所示:

<input type="password" name="PasswordInput" id="PasswordInput"/>

Html相关问答推荐

我能阻止浏览器跨跨区边界折叠空格吗?

为什么从输入步骤中的扣减不能按预期进行?

在元素之间添加空格

如何通过扭曲元素来倾斜元素?

未知高度正在应用于前标记

Bootstrap nav没有崩溃

现代网络浏览器可以并行下载哪些类型的网络资源?还有,什么东西不能并行下载?

轨道上的居中范围滑块拇指(Webkit)

为什么index.html在Django中有这样的名称?

如果最后一行的卡数少于其他行,则在使用flex grow使卡在调整大小时拉伸后,flex卡的宽度将被拉伸

使用不同字体对齐元素

Swift 以编程方式在 YouTube 嵌入视频中进入全屏

如何修剪 flex: 1 内的垂直文本?

为什么 html 元素的 max-width 和 width 一起工作?

可以通过悬停时的自定义区域更改文本属性吗?

我正在使用 NVDA 并多次读取关闭按钮,但它的读取非常完美

显示填充正方形的图形的简单页面的 HTML 代码

为什么在使用src与srcdoc时 iframe 内容高度呈现不同?

将表格标题和过滤器调整到表格的顶部边缘

CSS 变量不适用于自定义属性回退值