Event.currentTarget

Event 介面的唯讀屬性 currentTarget 會標明事件指向(current target)、還有該事件所遍歷的 DOM。屬性總會指向當時處理該事件的事件監聽器所註冊的 DOM 物件,而 event.target 屬性則是永遠指向觸發事件的 DOM 物件。

範例

event.currentTarget 在把相同的事件監聽器,附加到多個元素時,會出現很有趣的事情:

function hide(e){
  e.currentTarget.style.visibility = "hidden";
  // 在這個函式用於事件監聽器時: this === e.currentTarget
}

var ps = document.getElementsByTagName('p');

for(var i = 0; i < ps.length; i++){
  ps[i].addEventListener('click', hide, false);
}

// 單擊四周的話 p 元素就會消失

規範

規範 狀態 註解
DOM
The definition of 'Event.currentTarget' in that specification.
Living Standard
DOM4
The definition of 'Event.currentTarget' in that specification.
Obsolete
Document Object Model (DOM) Level 3 Events Specification
The definition of 'current event target' in that specification.
Obsolete
Document Object Model (DOM) Level 2 Events Specification
The definition of 'Event.currentTarget' in that specification.
Obsolete Initial definition

瀏覽器相容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
currentTargetChrome Full support YesEdge Full support 12Firefox Full support YesIE Full support 9
Full support 9
No support 6 — 9
Notes
Notes On Internet Explorer 6 through 8, the event model is different. Event listeners are attached with the non-standard EventTarget.attachEvent method. In this model, there is no equivalent to event.currentTarget and this is the global object. One solution to emulate the event.currentTarget feature is to wrap your handler in a function calling the handler using Function.prototype.call with the element as a first argument. This way, this will be the expected value.
Opera Full support YesSafari Full support 10WebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support 10Samsung Internet Android Full support Yes

Legend

Full support  
Full support
See implementation notes.
See implementation notes.

參見