event currentTarget changes after setTimeout

后端 未结 4 1729
南方客
南方客 2021-01-04 19:06

consider:



        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-04 19:32

    Inspired by Joseph Marikle's comment, I found the real cause:

    DOM Standard § 2.9. Dispatching events describes the algorithm, which I'm partially reproducing here:

    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.
    […]
    

    So, after the event has finished processing all phases (capture, at_target, bubbling), then currentTarget gets assigned null.

提交回复
热议问题