【发布时间】:2022-02-03 22:59:52
【问题描述】:
函数执行完成,不会再被调用,但我仍然可以访问其中的事件处理程序
(function () {
const header = document.querySelector('h1');
header.style.color = 'red';
header.addEventListener('click', function () {
this.style.color = this.style.color === 'blue' ? 'red' : 'blue';
});
})();
【问题讨论】:
-
所以附加到 dom 元素的事件总是保持活动状态,直到它们被删除?
标签: javascript function events closures handler