HTML input autofocus 属性

htmlweb developmentfront end technology更新于 2025/3/20 7:08:17

<input> 元素的 autofocus 属性用于指定网页加载时哪个 <input> 元素将获得焦点。此属性在 HTML5 中引入。

以下是语法 −

<input autofocus>

现在让我们看一个实现 <input> 元素 autofocus 属性的示例 −

示例

<!DOCTYPE html>
<html>
<body>
<h2>Log in to your account</h2>
<form action="" method="get">
   Id: <input type="text" name="id"><br>
   Password: <input type="password" name="pwd"><br>
   DOB: <input type="date" name="dob" autofocus><br>
   <button type="submit" value="Submit">Submit</button>
</form>
</body>
</html>

输出

在上面的例子中,我们创建了一个表单 −

<form action="" method="get">
   Id: <input type="text" name="id"><br>
   Password: <input type="password" name="pwd"><br>
   DOB: <input type="date" name="dob" autofocus><br>
   <button type="submit" value="Submit">Submit</button>
</form>

我们已将自动对焦设置为如下所示的第三个输入框,即 DOB,因此光标本身在该输入框中可见 −

DOB: <input type="date" name="dob" autofocus><br>

相关文章


有用资源