What is the difference between event bubbling and event capturing?

What is the difference between event bubbling and event capturing?

UMIDIGI Bison | English Review

Suppose there is a form tag inside a div tag and both have onclick event detectors. Event propagation describes how one event detector can be triggered after another.

Consider the following example. You have three nested elements: div, form, and button. Each element has a click event listener. The browser displays an alert with a message when each element is clicked.

To get the event capture, you only need to make one code change. The second parameter of addEventListener () sets the propagation type. It is false by default to represent bubbling. To enable event capture, you must set the second parameter to true.

You can stop event propagation using the stopPropagation () method. The addEventListener () method accepts an event name and a handler function. The handler takes an event object as a parameter. This object contains all the information about the event.

R

The window object, the document object and all the nodes of type Element of an HTML document can be targets of events. All these objects have a predefined method called addEventListener() that allows adding one, two or more event handlers to them.

Finally, theBooleanOptional parameter specifies whether the event should be captured (true) or should not be captured (false, which is the default value if this parameter is not specified. The third parameter we will not specify, or we will set it to false. The reason it is there is more historical than a real need and we will not dwell on this parameter.

Read more  What does D-BOX mean in movie theaters?

The expected result is that when the mouse is hovered over a title element <h1>, <h2>, etc. the color changes to orange. Once the mouse is no longer over the element, the text color will change to brown. Note that in some older browsers the code may not work.

Remember that within the response function to an event, the keyword this refers to the element that is the one who receives (target) the event. In the previous example we see an example with the code this.style.color = ‘orange’; that is in charge of changing the color of the text of the HTML element where the event has been generated.

Samsung Galaxy S21 Ultra | English Review

For example, if we have a single form.onclick handler, it can catch all clicks within the form. No matter where the click was made, it propagates to the <form> and executes the handler.

Usually there is no real need to prevent propagation, but a task that apparently requires it can be solved by other means. One of them is to use custom events, we’ll cover that later. We can also write our data to the event object in one handler and read it in another, so that we can pass information about the downstream process to the handlers in the parents.

It should be noted that while formally there are 3 phases, the 2nd phase (“the target phase”: the event reached the element) is not handled separately; the handlers in both the capture and propagation phases fire in that phase.

Read more  Why do shredders stop working?

Any event handler can stop the event by calling event.stopPropagation(), but it is not recommended because we can’t really ensure that we won’t need it later, perhaps to complete different things.

Genshin Impact WATERS Guide

You may want to extend the Button class to create something more elaborate. In this case, you can define default event behaviors for your class using the event handlers of the class.

This method is called when the user touches the item (in touch mode) or focuses on the item with the navigation keys or trackball and presses the appropriate “Enter” key or trackball.

This method is called when the user holds down the item (in touch mode) or focuses on the item with the navigation keys or trackball and holds down the appropriate “Enter” key or trackball (for one second).

This method is called when the user performs an action qualified as a touch event, e.g. press, release or any movement gesture on the screen (within the element boundaries).

Note that the onClick() callback in the above example does not have a value to display, but some other event listener object methods must display a Boolean value. The reason depends on the event. The reasons for the few cases that do are explained below:

Read more  What happens if a planning appeal is refused?