Event Flow in HTML
An event is a specific moment of interaction that occurs in a document or brower window and act as a bridge between JavaScript and DOM. Event Flow describes the order in which events are received from the page.
W3C Event Flow
The W3C specifies that the DOM event flow has three phases.
- The event capture phase.
- The in-target phase.
- The event bubbling phase.
addEventListener
Syntax
1 | target.addEventListener(type, listener [, options]); |
Parameters
type (String)
A case-sensitive string representing the event type to listen for.
listener (Object, Function)
An object that implements the EventListener interface, or a JavaScript function.
useCapture (Boolean)
The default value is false. If the value is false, the listener is executed during the bubbling phase. if true during the capture phase.
Events Executed Order
Capture stars at the outermost layer and bubbling from the response layer. But in the in-target phase, the order is determined by the code.
1 | // Event execution sequence is caputer -> aims -> bubble |
Click Page
1
2page: Caputer
page: bubbleClick Content
1
2
3
4
5// In the in-target phase, the order is determined by the code.
page: Caputer
content: bubble
content: Caputer
page: bubbleClick Buttom
1
2
3
4
5
6
7// In the in-target phase, the order is determined by the code.
page: Caputer
content: Caputer
buttom: bubble
buttom: Caputer
content: bubble
page: bubble
Event Agent
Event agent users the event bubbling to manage all events of a certain type by specifying only one event handler.
1 | <ul id="list"> |
Bind the event to the tag ‘ul’ to manage the tag ‘li’ event. This reduces memory consumption and efficiency.