表示事件物件在傳遞過程中,目前在處理事件之監聽器所在的元素節點。event.currentTarget
屬性總會指向現在正處理該事件的事件監聽器所註冊的 DOM 物件,而 event.target
屬性則是永遠指向觸發事件的 DOM 物件。
範例
event.currentTarget
is interesting to use when attaching the same event handler to several elements.
function hide(e){ e.currentTarget.style.visibility = "hidden"; // When this function is used as an event handler: this === e.currentTarget } var ps = document.getElementsByTagName('p'); for(var i = 0; i < ps.length; i++){ ps[i].addEventListener('click', hide, false); } // click around and make paragraphs disappear
規範
Specification | Status | Comment |
---|---|---|
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 |
瀏覽器相容性
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes)[1] | (Yes) | ? |
Feature | Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | ? | (Yes) | ? | ? | ? | ? |
[1] 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.