There are very important features in JavaScript events-event bubbling: Gossip Less, let's illustrate everything by observing the following code:
<div onclick= "alert (' The handler! ')" > <em> If you click on <code>em</code>, the handler on <code>DIV</code> runs </em></div>
The Click event is triggered whenever we click <div>, <em>, or <code>. Why do we click the EM or Code node to trigger the DIV's Click event? In fact, this is the bubbling characteristics, the principle of bubbling is very simple. Let's look at another piece of code:
<style>Body * {margin:10px;border:1px solid blue;} </style><form onclick= "alert (' form ')" >form<div onclick= "alert (' div ')" >div<p onclick= "alert (' P ')" >P</p></div></form>
If we click on P, the browser will execute alert (' P ') in turn; Alert (' div '); Alert (' form '). This means that when an event is triggered on a node, it first executes its own event, and then it finds its parent Div's event, then to the form, and eventually the event "bubbles" to the surface, which is its ancestor, the document object.
This process is called "bubbling", like a bubble in the water, is the bottom up. Note: Not all events are "bubbling", such as focus. The difference between event.target and this: Event.target: is the "target" element to find the initiating event, which is not changed during bubbling. This: refers to the current element for example: if we have a form.onclick function, it can capture all the click events within a form, regardless of where the click event is triggered in the form, it will bubble to the form layer and execute the onclick function. In the Form.onclick function, this (=event.currenttarget) refers to the <form> form, because this event.target refers to the element in the form that is actually clicked. Take a look at the following example, from this example to better understand the difference between this and event.target.
Form.onclick = function(event) {Event.target.style.backgroundColor = ' yellow ';//chrome needs some time to paint y Ellowsettimeout (() = {alert ("target =" + Event.target.tagName + ", this=" + this. TagName); Event.target.style . BackgroundColor = "}, 0);};
This.tagname: Refers to FORMevent.target.tagName: refers to which element we click.
Stop bubblingGenerally speaking,The bubbling event will rise from the target element to Use: Event.stoppropagation ()
<body onclick= "alert (' The bubbling doesn ' t reach here ')" ><button onclick= "event.stoppropagation ()" >click Me</button></body>
When you click on the Me button, the body does not have alert action. Event.stopimmediatepropagation ()if a single element is bound to handle multiple events, we use stoppropagation to stop one of the bubbles, but the other bubbles are still executing. To block the bubbling of the current element and the bound event, you need to use the event.stopimediatepropagation () function.
Note:there is usually no need to stop bubbles from happening. The general situation can be solved by other means. For example, the use of custom events or the ability to write data to an object in an event.
JavaScript event bubbling