Definition: Upward conduction of events, when an event of a descendant element is triggered, the same event of its ancestor element will also be triggered.
Prevent event bubbling:event.cancelBubble = true
8. Event delegation:
Definition: Bind the event to the common parent element uniformly. When the event of the descendant element is triggered, it will bubble up to the ancestor element, so as to process the event through the corresponding function of the ancestor element.
Description: The principle of event bubbling is used to reduce the number of event bindings and improve the performance of the program
Detail processing: If you only need to trigger events for the current text, not for the entire block, you need to use the event.targetcurrent click object to judge.
9. Event binding:
对象.事件名称 = 函数Use the form to bind the response function, it can only bind one function to one event of one element at the same time. If multiple functions are bound, the later functions will overwrite the previous ones.
Use addEventListener()to bind multiple response functions to an element at the same time, execution order: bind first, execute first.
parameter:
[The name of the event, without on, the type is a string]
[Callback function, called when an event is triggered]
[Whether to trigger in the capture phase, the type is a Boolean value, generally pass false]
IE8 and below do not support
函数中的this指向当前调用的对象
attachEvent()Multiple response functions can be bound to an element at the same time by using , execution order: bind first and then execute.
parameter:
[The name of the event, add on, the type is a string]
[Callback function, called when an event is triggered]
IE8 and below support
函数中的this指向window
10. Event dissemination:
Regarding the spread of events, Netscape and Microsoft have different views.
Microsoft Corporation: Events should be propagated from the inside out. When an event is triggered, the event of the current element should be triggered first, and then spread to the ancestor elements of the current element, that is, the event should be executed in the bubbling phase.
Netscape: Events should flow from the outside in. When an event is triggered, the event should be triggered on the outermost ancestor element of the current element first, and then propagated inward to descendant elements.
W3C combines the solutions of the two companies and divides event propagation into three stages:
捕获阶段: Capture events from the outermost ancestor element to the target element, but the event will not be triggered by default at this time.
目标阶段: The event captures the target element and starts firing events on the target element.
冒泡阶段: The event is passed from the target element to its ancestor elements, and the events on the ancestor elements are triggered in turn.
If you want to trigger events during the capture phase, you need将addEventListener()的第三个参数设置为true
IE8 and below browsers do not have a capture phase.