From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../web/api/globaleventhandlers/onfocus/index.html | 94 ++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 files/ja/web/api/globaleventhandlers/onfocus/index.html (limited to 'files/ja/web/api/globaleventhandlers/onfocus') diff --git a/files/ja/web/api/globaleventhandlers/onfocus/index.html b/files/ja/web/api/globaleventhandlers/onfocus/index.html new file mode 100644 index 0000000000..a602033ddf --- /dev/null +++ b/files/ja/web/api/globaleventhandlers/onfocus/index.html @@ -0,0 +1,94 @@ +--- +title: GlobalEventHandlers.onfocus +slug: Web/API/GlobalEventHandlers/onfocus +tags: + - API + - Event Handler + - GlobalEventHandlers + - HTML DOM + - Property + - Reference +translation_of: Web/API/GlobalEventHandlers/onfocus +--- +
{{ApiRef("HTML DOM")}}
+ +

{{domxref("GlobalEventHandlers")}} ミックスインの onfocus プロパティは、与えられた要素上の focus イベントを処理する {{domxref("EventHandler")}} です。

+ +

focus イベントは、ユーザーがある要素にフォーカスを設定した時に生じます。

+ +

非 input 要素上で onfocus を発生させるには、{{htmlattrxref("tabindex")}} 属性が与えられていなければなりません (詳細は Building keyboard accessibility back in を参照)。

+ +
+

補足: onfocus の反対が {{domxref("GlobalEventHandlers.onblur", "onblur")}} です。

+
+ +

構文

+ +
target.onfocus = functionRef;
+
+ +

+ +

functionRef は関数名または 関数式 です。この関数は、{{domxref("FocusEvent")}} オブジェクトとその 1 個の引数を受け取ります。

+ +

+ +

この例は、{{domxref("GlobalEventHandlers.onblur", "onblur")}} および onfocus を使用して {{HtmlElement("input")}} 要素内のテキストを変更します。

+ +

HTML

+ +
<input type="text" value="ここをクリック">
+
+ +

JavaScript

+ +
let input = document.querySelector('input');
+
+input.onblur = inputBlur;
+input.onfocus = inputFocus;
+
+function inputBlur() {
+  input.value = 'Focus has been lost';
+}
+
+function inputFocus() {
+  input.value = 'Focus is here';
+}
+ +

実行結果

+ +

フォームフィールドの内側と外側をクリックしてみてください。それに応じてコンテンツが変化するか観察してください。

+ +

{{EmbedLiveSample('Example')}}

+ +

仕様

+ + + + + + + + + + + + + + +
仕様書策定状況備考
{{SpecName('HTML WHATWG','webappapis.html#handler-onfocus','onfocus')}}{{Spec2('HTML WHATWG')}}
+ +

ブラウザー実装状況

+ + + +

{{Compat("api.GlobalEventHandlers.onfocus")}}

+ +

IE とは対称的に、focus イベントを受け取るほとんどすべての要素では、Gecko ブラウザー上のほとんどすべての要素において、このイベントが動作しません。

+ +

関連項目

+ + -- cgit v1.2.3-54-g00ecf