gpt4 book ai didi

javascript - setTimeout 后事件 currentTarget 发生变化

转载 作者:太空狗 更新时间:2023-10-29 13:51:02 27 4
gpt4 key购买 nike

考虑:

let sel=document.getElementById('mys');

sel.onchange=function(e) {
console.log(e.currentTarget===null); // false
setTimeout(e => {
console.log(e.currentTarget===null); // true
}, 0, e);
}
<select id="mys">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>

  • 为什么 e.currentTarget 在超时后会发生变化?这是浏览器 (chrome) 错误吗?

  • 如何将事件的精确克隆传输到超时函数?我尝试了简单的克隆,但 currentTarget 不可写且不能被覆盖..

最佳答案

灵感来自 Joseph Marikle's comment ,我找到了真正的原因:

DOM Standard § 2.9. Dispatching events描述了我在此处部分复制的算法:

To **dispatch** an event to a target […] run these steps:
[…]
5. If target is not relatedTarget or target is event’s relatedTarget, then:
[…]
9. While parent is non-null:
[…]
8. Otherwise, set target to parent and then:
[…]
9. If parent is non-null, then set parent to the result of invoking parent’s get the parent with event.
[…]
[…]
12. Set event’s eventPhase attribute to CAPTURING_PHASE.
[…]
14. For each tuple in event’s path, in reverse order:
1. […] invoke […]
15. For each tuple in event’s path, in order:
1. If tuple’s target is non-null, then set event’s eventPhase attribute to AT_TARGET.
2. Otherwise, set event’s eventPhase attribute to BUBBLING_PHASE.
3. […] invoke […]
[…]
6. Set event’s eventPhase attribute to NONE.
7. Set event’s currentTarget attribute to null.
[…]

To **invoke** […] run these steps:
[…]
5. Initialize event’s currentTarget attribute to struct’s invocation target.
[…]

因此,在事件处理完所有阶段(捕获、at_target、冒泡)后,currentTarget 将被分配为 null

关于javascript - setTimeout 后事件 currentTarget 发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39649156/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com