JavaScript 事件对象 currentTarget 可能为null 作者:马育民 • 2024-10-18 12:39 • 阅读:10002 # 提出问题1 ``` var button = document.getElementById("btn") button.addEventListener("click", function(e) { console.log(e); // 在控制台展开时,currentTarget 为null console.log(e.currentTarget); // 这里 currentTarget 有值 }); ``` # 提出问题2 ``` var button = document.getElementById("btn") button.addEventListener("click", function(e) { setTimeout(() => { console.log('event.currentTarget:', event.currentTarget) // currentTarget 为null }, 0); }); ``` # 原因 `currentTarget` 是一个 **实时值** ,只存在 **事件触发 到 事件处理结束** 之间,随后就重置为 `null` 了 所以,在控制台展开时,为 `null`;延时执行后,也为 `null` 参考: https://juejin.cn/post/6900534814319837191 https://blog.csdn.net/weixin_44256803/article/details/119145977 https://segmentfault.com/q/1010000005011211 原文出处:http://malaoshi.top/show_1IX8d0jslKmU.html