事件:target 属性
注意:此功能在 Web Workers 中可用。
Event 接口的只读 target 属性是对事件分派到的对象的引用。当在事件的冒泡或捕获阶段调用事件处理程序时,它与 Event.currentTarget 不同。
值
相关的 EventTarget。
示例
event.target 属性可用于实现事件委托。
js
// 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) {
// evt.target refers to the clicked <li> element
// This is different from evt.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);
规范
| 规范 |
|---|
| DOM # ref-for-dom-event-target③ |
浏览器兼容性
加载中…