That is the difference between Why is addEventListener needed?Let’s take a look at a clip first: <div id="box">Test</div> Use on code: windwo.onload = function(){ var box = document.getElementById("box"); box.onclick = ()=>console.log("I am box1"); box.onclick = ()=>console.log("I am box2"); } // Run result: I am box2 You see, the second onclick event covers the first onclick. Although in most cases we can use on to achieve the desired effect, sometimes we need to execute multiple identical events, which is obviously impossible to achieve using on. However, you can use addEventListener to bind the same event multiple times without overwriting the previous event. Code using addEventListenerwindow.onload = function(){ var box = document.getElementById("box"); box.addEventListener("click",()=>console.log("I am box1")); box.addEventListener("click",()=>console.log("I am box2")); } // Operation result: I am box1 //I am box2 The first parameter of Use of the third parameterSometimes the situation is like this: <body> <div id = "box"> <div id = "child"></div> </div> </body> If I add a cclick event to the box, there is no problem if I click the box directly, but if I click the child element, how does it execute? box.addEventListener("click",()=>console.log("box")); child.addEventListener("click",()=>console.log("child")); // Execution result: child -> box That is to say, the default is to follow the order of event bubbling execution. If the third parameter is true, the execution will be performed in the order in which the events are captured. Summarize 1. The 2. 3. 4. This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: Solution to failure in connecting to mysql in docker
Table of contents Method 1: Call the function dir...
Table of contents 1. How to monitor Tomcat 2. Jav...
Directly code: select 'bigint unsigned' a...
Prepare the bags Install Check if Apache is alrea...
Overflow Hide It means hiding text or image infor...
In our life, work and study, social networks have ...
Step 1: Add a secondary domain name to the Alibab...
In the previous article, we introduced three comm...
1.1 Download the binary installation package wget...
1. flex-direction: (direction of element arrangem...
To use Nginx under Windows, we need to master som...
First look at the code and effect↓ <style> ...
.NET SDK Download Link https://dotnet.microsoft.c...
Flash enabled designers and developers to deliver...
In the field of data analysis, database is our go...