Movatterモバイル変換


[0]ホーム

URL:


  1. 開発者向けのウェブ技術
  2. Web API
  3. Event
  4. currentTarget

このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docsコミュニティーについてもっと知り、仲間になるにはこちらから。

View in EnglishAlways switch to English

Event: currentTarget プロパティ

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.

メモ: この機能はウェブワーカー内で利用可能です。

currentTargetEvent インターフェイスの読み取り専用プロパティで、イベントハンドラーが取り付けられた要素を識別します。

これは、イベントが発行された要素と常に同じであるとは限りません。イベントは、ハンドラーを持つ要素の子孫で発生し、ハンドラーを持つ要素にバブルアップされる可能性があるからです。イベントが発行された要素は、Event.target で指定されます。

なお、currentTarget の値はイベントハンドラー内でのみ利用できます。イベントハンドラー外ではnull となります。つまり、例えばイベントハンドラー内でEvent オブジェクトの参照を取得し、その後イベントハンドラー外でそのcurrentTarget プロパティにアクセスすると、その値はnull となります。

EventTarget で、現在のイベントハンドラーが装着されているオブジェクトを表します。

currentTarget と target

この例は、currentTargettargetの違いを示しています。

HTML

このページには、 "parent" の<div> の中に "child" の<div> があります。

html
<div>  parent をクリック  <div>child クリック</div></div><button>リセット</button><pre></pre>
button,div,pre {  margin: 0.5rem;}div {  padding: 1rem;  border: 1px solid black;}

JavaScript

イベントハンドラーは親要素に装着されています。イベントハンドラーは、event.currentTargetevent.target の値をログ出力します。

「リセット」ボタンも備えており、これは例を再読み込みするだけです。

js
const output = document.querySelector("#output");const parent = document.querySelector("#parent");parent.addEventListener("click", (event) => {  const currentTarget = event.currentTarget.getAttribute("id");  const target = event.target.getAttribute("id");  output.textContent = `Current target: ${currentTarget}\n`;  output.textContent += `Target: ${target}`;});const reset = document.querySelector("#reset");reset.addEventListener("click", () => document.location.reload());

結果

子要素の<div> 内をクリックすると、target は子要素を示します。親要素の<div> 内をクリックすると、target は親要素を示します。

どちらの場合も、currentTarget は親を特定します。ハンドラーが装着されている要素だからです。

仕様書

Specification
DOM
# ref-for-dom-event-currenttarget②

ブラウザーの互換性

関連情報

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp