HTMLオートフォーカス属性
定義と使用法
autofocus
属性はブール属性です。
存在する場合、ページの読み込み時に要素が自動的にフォーカスを取得するように指定します。
に適用されます
この
autofocus
属性は、次の要素で使用できます。
要素 | 属性 |
---|---|
<ボタン> | オートフォーカス |
<入力> | オートフォーカス |
<選択> | オートフォーカス |
<textarea> | オートフォーカス |
例
ボタンの例
オートフォーカス付きのボタン:
<button type="button" autofocus>Click Me!</button>
入力例
ページが読み込まれたときに、「名」入力フィールドに自動的にフォーカスが移るようにします。
<form action="/action_page.php">
First name: <input type="text" name="fname" autofocus><br>
Last name: <input type="text" name="lname"><br>
<input type="submit">
</form>
Textareaの例
オートフォーカス付きのテキスト領域:
<textarea autofocus>
At w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.
</textarea>
ブラウザのサポート
このautofocus
属性では、要素ごとに次のブラウザがサポートされています。
Element | |||||
---|---|---|---|---|---|
form | 5.0 | 10.0 | 4.0 | 5.0 | 9.6 |
input | 5.0 | 10.0 | 4.0 | 5.0 | 9.6 |
textarea | Yes | 10.0 | 4.0 | Yes | Yes |