入力フィールドがフォーカスを取得したときに JavaScript を実行します:
<input type="text" id="fname" onfocus="myFunction(this.id)">
Try it Yourself ❯
他の "Try it Yourself" の例が下にあります。
onfocus 属性は、要素がフォーカスを取得した瞬間に発火します。
Onfocus は、大抵の場合 <input>、<select>、<a> で使用します。
チップ: onfocus 属性は、onblur 属性の反対です。
イベント属性 | |||||
---|---|---|---|---|---|
onfocus | Yes | Yes | Yes | Yes | Yes |
なし。
<element onfocus="script">
値 | 説明 |
---|---|
script | script は onfocus で実行される |
サポートする HTML タグ: | 次の要素を除くすべての HTML 要素: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title> |
---|
"onblur" と一緒に "onfocus" 属性を使用します:
<input type="text" onfocus="focusFunction()" onblur="blurFunction()">
Try it Yourself ❯
HTML DOM リファレンス:onfocus イベント