Home >Web Front-end >JS Tutorial >Why does the event bubbling mechanism trigger twice?
Why does event bubbling happen twice in a row?
Event bubbling is an important concept in web development. It means that when an event is triggered in a nested HTML element, the event will bubble up from the innermost element to the outermost element. layer elements. This process can sometimes cause confusion. One common problem is that event bubbling occurs twice in a row.
In order to better understand why event bubbling occurs twice in a row, let's first look at a code example:
<div id="outer"> <div id="inner"> <button id="button">Click me</button> </div> </div> <script> document.getElementById('button').addEventListener('click', function() { console.log('Button clicked'); }); document.getElementById('inner').addEventListener('click', function() { console.log('Inner div clicked'); }); document.getElementById('outer').addEventListener('click', function() { console.log('Outer div clicked'); }); </script>
The above code creates a nested HTML structure that contains a The outermost <div> element (id="outer"), a nested <code><div> element (id="inner"), and an Button element (id="button"). <p>In this code, we add a click event listener for each element to output information about the click of the corresponding element. When we click the button on the page, the output we expect is: </p><pre class='brush:php;toolbar:false;'>Button clicked
Inner div clicked
Outer div clicked</pre><p> However, the actual output is: </p><pre class='brush:php;toolbar:false;'>Button clicked
Inner div clicked
Outer div clicked
Inner div clicked
Outer div clicked</pre><p> As you can see, the event bubbling will occur twice in a row. times, causing the event handling function to be executed repeatedly. </p>
<p>The root cause of this problem lies in the execution order of the event bubbling phase. In the bubbling phase, the event will bubble up from the innermost element to the outer element, and then execute the event handler function of the parent element step by step. Therefore, when we click the button, the click event will first trigger the button's event handler, then bubble up to the nested <code><div> element, and continue executing the element's event handler. However, since the <code><div> element is also nested in the outermost <code><div> element, the event will bubble up to the outermost element again, resulting in The outermost event handler function is executed again. <p>There are many ways to solve this problem. Let’s introduce two commonly used methods: </p>
<ol><li>Stop event bubbling: </li></ol>
<p>In Calling the <code>event.stopPropagation()
method in the event processing function can prevent further propagation of the event, that is, stop event bubbling. Modify the event handler function of the button in the above code example as follows:
document.getElementById('button').addEventListener('click', function(event) { console.log('Button clicked'); event.stopPropagation(); // 阻止事件冒泡 });
After using the event.stopPropagation()
method, the event bubbling will stop on the button element and will not propagate to the embedded element. On the nested <div> element and the outermost <code><div> element. Therefore, the event processing function will only be executed once, and the output result is: <pre class='brush:php;toolbar:false;'>Button clicked</pre><ol start="2"><li>Monitoring capture phase: </li></ol>
<p>In addition to the event bubbling phase, DOM events have another capture phase. The capture phase refers to the process of events propagating from the outermost element to the inner element. Using the capture phase, you can add an event listener on the outermost element, handle the event in the capture phase, and then decide whether to execute event handlers on other elements. Modify the event handler function of the outermost <code><div> element in the above code example as follows: <pre class='brush:javascript;toolbar:false;'>document.getElementById('outer').addEventListener('click', function() {
console.log('Outer div clicked');
}, true); // 添加 true 参数表示监听捕获阶段
</pre><p>By adding the parameter <code>true
to the outermost element Event listeners can execute event processing functions during the capture phase. In this way, during the event bubbling stage, the event will not trigger the event processing function on the outermost element again, thus avoiding repeated execution.
To summarize, event bubbling will occur twice in a row because the event will bubble from the innermost element to the outermost element during the bubbling phase, and the event processing function on each element will be executed. . In order to solve this problem, we can use the event.stopPropagation()
method to prevent the event from bubbling, or process the event by listening to the capture phase to avoid repeated execution.
The above is the detailed content of Why does the event bubbling mechanism trigger twice?. For more information, please follow other related articles on the PHP Chinese website!