currentTarget 只读特性在 事件 interface identifies the current target for the event, as the event traverses the DOM. It always refers to the element to which the event handler has been attached, as opposed to Event.target , which identifies the element on which the event occurred and which may be its descendant.

句法

var currentEventTarget = event.currentTarget;
					

EventTarget

范例

Event.currentTarget is interesting to use when attaching the same event handler to several elements.

function hide(e){
  e.currentTarget.style.visibility = 'hidden';
  console.log(e.currentTarget);
  // 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++){
  // Console: print the clicked <p> element
  ps[i].addEventListener('click', hide, false);
}
// Console: print <body>
document.body.addEventListener('click', hide, false);
// Click around and make paragraphs disappear
					

注意: event.currentTarget is only available while the event is being handled. If you console.log() the event object, storing it in a variable, and then look for the currentTarget key in the console, its value will be null . Instead, you can either directly console.log(event.currentTarget) to be able to view it in the console or use the debugger statement, which will pause the execution of your code thus showing you the value of event.currentTarget .

规范

规范 状态 注释
DOM
The definition of 'Event.currentTarget' in that specification.
实时标准
DOM4
The definition of 'Event.currentTarget' in that specification.
过时
DOM (文档对象模型) 3 级事件规范
The definition of 'current event target' in that specification.
过时
DOM (文档对象模型) 2 级事件规范
The definition of 'Event.currentTarget' in that specification.
过时 初始定义

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
currentTarget Chrome 1 Edge 12 Firefox 1 IE 9
9
不支持 6 — 9
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 7 Safari 10 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 10 Samsung Internet Android 1.0

图例

完整支持

完整支持

见实现注意事项。

另请参阅

元数据

  • 最后修改: