Reverse event bubbling in javascript

后端 未结 3 1024
时光说笑
时光说笑 2021-02-08 00:45

As you know, events usually bubble up in javascript, so the event handler of the element that fires the event is executed first, then the event handler of the parent element is

3条回答
  •  南笙
    南笙 (楼主)
    2021-02-08 01:37

    Reverse event bubbling is called capture phase.

    See the DOM event architecture

    Pass true as the 3rd argument to Event.addEventListener to have it trigger on capture phase

    el.addEventListener("click", function () {
      console.log("i run in capture");
    }, true);
    

    Of course it won't work in legacy platforms. You'll need a DOM3 events shim to emulate the event system. Feel free to contribute to the DOM-shim

提交回复
热议问题