Sequence of event flow

The

js section is simple enough to show the order in which the event flow is executed.

window.onload = function(){
    var outA = document.getElementById("outA");  
    var outB = document.getElementById("outB");  
    var outC = document.getElementById("outC");  
    outA.addEventListener("click",function(){alert("bubble1");},false);
    outB.addEventListener("click",function(){alert("bubble2");},false);
    outB.addEventListener("click",function(){alert("capture2");},true);    
    outC.addEventListener("click",function(){alert("target");},true);
    outA.addEventListener("click",function(){alert("capture1");},true);
};

css is used to render these three div

div-sharpoutA{
    width:400px; height:400px; background:-sharpCDC9C9;position:relative;
}
div-sharpoutB{
    height:200; background:-sharp0000ff;top:100px;position:relative;
}
div-sharpoutC{
    height:100px; background:-sharpFFB90F;top:50px;position:relative;
}

structure is simpler, three div

<div id="outA">
    <div id="outB">
        <div id="outC">
        </div> 
    </div>
</div>

when clicking on divB, my understanding is that the output should be
capture1--capture2--bubble2--bubble1

.

Why is the actual result
capture1--bubble2--capture2--bubble1?

Oct.20,2021

1, window propagates to the place where the event is triggered, and the capture event is triggered when it encounters a registration.
2. Event that triggers registration when propagated to the event trigger.
3. Event triggers propagate to window and will be triggered when a registered bubbling event is encountered.

divB is where the event is triggered, and the order in which the event is triggered is determined by the registration order of the event.
your code first registers bubble2 , then capture2 . You can change the order and try again.


flips over the "js elevation" again, and the event flow is divided into three stages: event capture, target, and event bubbling. The DOM2 level event standard makes it clear that the capture phase does not involve event targets .

so when you click outB , although the event in the capture phase is specified with the true parameter, it cannot be triggered during the capture phase, because it is the event target itself. Js elevation says: "IE9, Safari, Chrome, Firefox, and Opera9.5 and later versions all trigger the time on the event object during the capture phase. As a result, there are two opportunities to manipulate the event on the target object." in fact, it seems that the high-version browser is more in line with the standard.

I tested on chrome . At this time, the firing order of events on outB is only related to the binding order. You might as well swap the order of the two event bindings on outB , and you will find that the result is capture1--capture2--bubble2--bubble1 .

Menu