HTML autofocus 屬性
定義和用法
autofocus
屬性是一個布林屬性。
如果存在,它指定當頁面載入時,元素應自動獲得焦點。
適用範圍
autofocus
屬性可用於以下元素
元素 | Attribute |
---|---|
<button> | autofocus |
<input> | autofocus |
<select> | autofocus |
<textarea> | autofocus |
示例
Input 示例
讓“名字”輸入欄位在頁面載入時自動獲得焦點
<form action="/action_page.php">
名: <input type="text" name="fname" autofocus><br>
姓:<input type="text" name="lname"><br>
<input type="submit">
</form>
自己動手試一試 »
Textarea 示例
帶 autofocus 的文字區域
<textarea autofocus>
在 w3schools.com,您將學習如何建立網站。我們提供所有 Web 開發技術的免費教程。
</textarea>
自己動手試一試 »
瀏覽器支援
autofocus
屬性對每個元素都有以下瀏覽器支援
元素 | |||||
---|---|---|---|---|---|
form | 5.0 | 10.0 | 4.0 | 5.0 | 9.6 |
input | 5.0 | 10.0 | 4.0 | 5.0 | 9.6 |
textarea | 是 | 10.0 | 4.0 | 是 | 是 |