target 特性为 事件 interface is a reference to the object onto which the event was dispatched. It is different from Event.currentTarget when the event handler is called during the bubbling or capturing phase of the event.

句法

const theTarget = someEvent.target;
					

EventTarget

范例

event.target property can be used in order to implement event delegation .

// Make a list
const ul = document.createElement('ul');
document.body.appendChild(ul);
const li1 = document.createElement('li');
const li2 = document.createElement('li');
ul.appendChild(li1);
ul.appendChild(li2);
function hide(evt) {
  // e.target refers to the clicked <li> element
  // This is different than e.currentTarget, which would refer to the parent <ul> in this context
  evt.target.style.visibility = 'hidden';
}
// Attach the listener to the list
// It will fire when each <li> is clicked
ul.addEventListener('click', hide, false);
					

规范

规范 状态 注释
DOM
The definition of 'Event.target' in that specification.
实时标准
DOM4
The definition of 'Event.target' in that specification.
过时
DOM (文档对象模型) 2 级事件规范
The definition of 'Event.target' 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
target Chrome 1 Edge 12 Firefox 1 IE 9 Opera 7 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0

图例

完整支持

完整支持

Compatibility notes

On IE 6–8, the event model is different. Event listeners are attached with the non-standard EventTarget.attachEvent() 方法。

In this model, the event object has a Event.srcElement property (instead of the target property) and it has the same semantics as Event.target .

function hide(evt) {
  // Support IE6-8
  var target = evt.target || evt.srcElement;
  target.style.visibility = 'hidden';
}
					

另请参阅

元数据

  • 最后修改: