考虑:
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>
最佳答案
受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/