JavaScript Event Delegation, and event.target vs. event.currentTarget
In this case, at the time you call console.log(e), there's a DOM element in the currentTarget property. But sometime later, that property is reset to null for some reason. When you expand the event object, that's what you see.
你的情况是,当调用console.log(e)时,currentTarget属性是有值的,但是过后这个值就被重置为null了。所以当你展开事件对象,看到的就是null。
<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
#wrapDiv,
#innerP,
#textSpan {
margin: 5px;
padding: 5px;
box-sizing: border-box;
cursor: default;
} #wrapDiv {
width: 300px;
height: 300px;
border: indianred 3px solid;
} #innerP {
width: 200px;
height: 200px;
border: hotpink 3px solid;
} #textSpan {
display: block;
width: 100px;
height: 100px;
border: orange 3px solid;
}
</style>
</head> <body>
<div id="wrapDiv">wrapDiv
<p id="innerP">innerP
<span id="textSpan">textSpan</span>
</p>
</div>
<script>
var div = document.getElementById('wrapDiv');
var p = document.getElementById('innerP');
var span = document.getElementById('textSpan'); div.onclick = function(ev){
console.log(ev); //
console.log("target:", ev.target);
console.log("currentTarget:", ev.currentTarget);
}
</script>
</body> </html>

-----------------------------------------------
Event delegation is a popular methodology in JavaScript. It allows us to add an event listener to one parent, and avoid to add many event listeners to specific nodes. We can demonstrate this technique with simple example.
Let’s say we have a list with thousands of items:
<body>
<div id="container">
<ul id="list">
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
..................................
<li><a href="#">Item 1000</a></li>
</ul>
</div>
</body>
With such number of items, it would be a nightmare to loop through every <a> element on the page, adding an event listener one after one. Moreover, it may “freeze” the page when JavaScript is trying to create them all.
So here comes the event delegation: When the event bubbles up to the body element, we can check the element that triggered the event, using the event object’s target property.
document.addEventListener("click", function(e) {
if(e.target && e.target.nodeName == "A") {
console.log("List item ", e.target.textContent, " was clicked!");
}
});
// When we click the 2nd item, the page prints out:
"List item Item 2 was clicked!"
target vs. currentTarget
Since we already talked about the event.target property, there is another property called event.currentTarget in JavaScript event. It can be very confused by just reading about them on JavaScript documentation.
As we’ve seen from the last example, when we clicked the a element, clickevent bubbles up to <body> node of the document like below:
<a> → <li> → <ul> → <div> → <body>
Let’s add one more line of code and prints out what the e.currentTarget is from the example we used above:
document.addEventListener(“click”, function(e) {
if(e.target && e.target.nodeName == “A”) {
console.log(“List item “, e.target.textContent, “ was clicked!”); // "List item Item 2 was clicked!"
}
console.log(e.currentTarget); // #document
});
It prints out “document” since we attached current event listener to the document while e.target refers to <a> which we clicked.
We can also look at one more example to see the differences between target and currentTarget. This time, we add the event listener to the <ul>:
document.getElementById(“list”).addEventListener(“click”, function(e) {
console.log(e.currentTarget); //<ul><li>...</li><ul>
console.log(e.target); //<a href="#">Item 2</a>
);
Again, the currentTarget refers to the element that the event listener directly attached to while the target still refers to the specific <a> we clicked.
With these two properties target and currentTarget, we can easily manipulate the node when the event gets triggered, as well as the node the event is attached to.
JavaScript Event Delegation, and event.target vs. event.currentTarget的更多相关文章
- Event对象中的target属性和currentTarget属性的区别
先上结论: Event.target:触发事件的元素: Event.currentTarget:事件绑定的元素: 通过下面的例子来理解这两个属性的区别: 使用Event.target属性的例子:(我在 ...
- JavaScript Interview Questions: Event Delegation and This
David Posin helps you land that next programming position by understanding important JavaScript fund ...
- javascript 事件委托 event delegation
事件委托 event delegation 一.概念: 假设我们有很多个子元素,每个元素被点击时都会触发相应事件,普通的做法是给每个子元素添加一个事件监听. 而,事件委托则是给它们的父元素添加一个事件 ...
- javascript事件代理(Event Delegation)
看了几篇文章,放上来供参考 司徒正美的文章,Event Delegation Made Easy --------------------------------------------------- ...
- JavaScript------事件委托(event delegation)
简单的说,事件委托(event delegation)是在DOM上层(也就是在触发事件的元素的父元素上)定义事件的处理程序,而不是定义在触发事件的元素本身上. 首先我们来举这样一个例子:我有N个li元 ...
- window.event.srcElement与window.event.target 触发事件的元素 触发事件对象的获取,window.event与时间函数参数的event是同一个 事件对象
判断事件触发的元素: var tag = window.event.target || window.event.srcElement; if (tag.tagName.toLowerC ...
- JS:event对象下的target属性和取消冒泡事件
1.target 通过获取DOM元素 var box = document.getElementById("box"); document.box.onclick = functi ...
- 【前端】event.target 和 event.currentTarget 的区别
event.target 和 event.currentTarget 的区别 举例说明: <!DOCTYPE html> <html> <head> <tit ...
- javascript运行模式:并发模型 与Event Loop
看了阮一峰老师的JavaScript 运行机制详解:再谈Event Loop和[朴灵评注]的文章,查阅网上相关资料,把自己对javascript运行模式和EVENT loop的理解整理下,不一定对,日 ...
随机推荐
- Python内存管理方式和垃圾回收算法解析
在列表,元组,实例,类,字典和函数中存在循环引用问题.有 __del__ 方法的实例会以健全的方式被处理.给新类型添加GC支持是很容易的.支持GC的Python与常规的Python是二进制兼容的. 分 ...
- BZOJ 2726: [SDOI2012]任务安排 [斜率优化DP 二分 提前计算代价]
2726: [SDOI2012]任务安排 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 868 Solved: 236[Submit][Status ...
- Easy File Sharing Web Server 6.9远程溢出漏洞
from struct import pack import socket,sys import os host="192.168.109.129" port=80 junk0 = ...
- C#中常见的系统内置委托用法详解(抄录)
C#中常见的系统内置委托 Action类.Func类.Predicate<T>.Comparison<T>委托 Action类的委托 Action委托 封装一个方法,该方法不具 ...
- Java_JSP自定义标签的开发与应用
在JSTL提供了四个标签库(核心标签库.国际化标签库.数据库标签库和XML标签库),涉及到了几十个标签.虽然这些标签可以完成比较复杂的工作,但它们仍然无法满足程序中的特殊需求.因此,就需要用户根据自己 ...
- 使用matplotlib的示例:调整字体-设置colormap和colorbar
使用matplotlib的示例:调整字体-设置colormap和colorbar # -*- coding: utf-8 -*- #********************************** ...
- Windows Apache Django 配置
所有刚刚学习Django 都会在这里配置的时候遇到问题.这里我简单秒速一个傻瓜步骤,使得大家更容易上手. 一.环境准备 1.windows 32系统 至少是 XPSP3 版本,注意:apache2.X ...
- XStream转换Java对象与XML
1.引入需要的jar包,在pom.xml中配置依赖 <dependency> <groupId>com.thoughtworks.xstream</groupId> ...
- 用Parallel.For()和Parallel.For<TLocal>()方法实现并行运行迭代
Parallel类是.NET 4中新增的抽象线程类.如果你开发用的是VS2008或更低版本,那么就直接关闭吧,下面两个示例用了匿名委托,如果不知道匿名委托的语法,那么先去简单了解一下,不然很难理解示例 ...
- RobotFramework自动化1-环境搭建
前言 Robot Framework是一款python编写的功能自动化测试框架.具备良好的可扩展性,支持关键字驱动,可以同时测试多种类型的客户端或者接口,可以进行分布式测试执行. Robot Fram ...