HTML onfocus イベント属性

❮ HTML イベント属性

入力フィールドがフォーカスを取得したときに 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

HTML 4.01 と HTML5 の相違点

なし。


構文

<element onfocus="script">

属性値

説明
script script は onfocus で実行される

技術的詳細

サポートする HTML タグ: 次の要素を除くすべての HTML 要素: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title>

Examples

その他の例

"onblur" と一緒に "onfocus" 属性を使用します:

<input type="text" onfocus="focusFunction()" onblur="blurFunction()">
Try it Yourself ❯

関連ページ

HTML DOM リファレンス:onfocus イベント


HTML Event Attributes HTML イベント属性