HTML autofocus 属性

htmlweb developmentfront end technology更新于 2026/1/30 3:24:17

HTML 中的 autofocus 属性可用于多个元素,并在页面加载时将焦点设置到特定元素上。构成 autofocus 属性的元素包括:

<button>、<input>、<select> 和 <textarea>。

现在让我们看一个在 HTML 中实现 autofocus 属性的示例 −

示例

<!DOCTYPE html>
<html>
<body>
<h2>Result</h2>
<form action="" accept-charset="ISO-8859-1">
   <label for="stinfo">Student:</label>
   <input type="text" name ="stinfo"><br>
   <label for="sub">Subject:</label>
   <input type="text" name ="sub" autofocus><br>
   <label for="rank">Rank:</label>
   <input type="number" name ="rank"><br>
   <label for="marks">Marks:</label>
   <input type="number" name ="marks"><br><br>
   <input type="submit">
</form>
</body>
</html>

输出

这将产生以下输出,显示焦点在第二个文本框上,因为我们已在相同的输入文本上设置了自动对焦 −


相关文章


有用资源