autofocus 属性は、ブール型属性です。
指定された場合、ページ読み込み時に、その要素が自動的にフォーカスを持つべきであることを指定します。
autofocus 属性は、以下の要素で使用することができます:
要素 | 属性 |
---|---|
<button> | autofocus |
<input> | autofocus |
<keygen> | autofocus |
<select> | autofocus |
<textarea> | autofocus |
ページ読み込み時に、"First name" 入力フィールドが自動的にフォーカスを持つようにしてみましょう:
<form action="demo_form.html">
First name: <input type="text" name="fname" autofocus><br>
Last name: <input type="text" name="lname"><br>
<input type="submit">
</form>
Try it Yourself ❯
ページ読み込み時に、"Encryption" フィールドが自動的にフォーカスを持つようにしてみましょう:
<form action="demo_keygen.html" method="get">
Username: <input type="text" name="usr_name">
Encryption: <keygen name="security" autofocus>
<input type="submit">
</form>
Try it Yourself ❯
autofocus 付きのテキスト・エリア:
<textarea autofocus>
At w3schools.com you will learn how to make a website. We offer free
tutorials in all web development technologies.
</textarea>
Try it Yourself ❯
autofocus
属性は、要素ごとに以下のブラウザがサポートしています。
要素 | |||||
---|---|---|---|---|---|
form | 5.0 | 10.0 | 4.0 | 5.0 | 9.6 |
input | 5.0 | 10.0 | 4.0 | 5.0 | 9.6 |
keygen | Yes | Not supported | Not supported | 6.0 | Yes |
textarea | Yes | 10.0 | 4.0 | Yes | Yes |