JS events refer to behaviors that occur on browser forms or HTML elements and are willing to trigger the execution of JS code blocks. Next, let's take a look at the related events. 1. Form eventsFor example, the onload event: when the page is fully loaded (including images, js files, css files, etc.), this event will be triggered. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Flower Dog Online</title> <script> window.onload = function(){ var mydiv = document.getElementById("mydiv"); alert("Page loading completed, the content of mydiv is: "+mydiv.innerText); } </script> </head> <body> <div id='mydiv'>I am a flower dog, a flower of a flower dog, a dog of a flower dog. </div> </body> </html>
2. Mouse eventsFor example, when the mouse clicks the left button, an onclick event occurs: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Flower Dog Online</title> <style> html,body{ width: 100%; height: 100%; } </style> <script> function mouseclick(){ alert('Page clicked'); } </script> </head> <body onclick="mouseclick()"> <div id='mydiv'>I am a flower dog, a flower of a flower dog, a dog of a flower dog. </div> </body> </html>
3. Keyboard events<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Flower Dog Online</title> <style> html,body{ background-color: aquamarine; width: 100%; height: 100%; } </style> <script> function keydown(event){ if(event.keyCode==37){ alert(event.keyCode +'You pressed the left button'); } if(event.keyCode==38){ alert(event.keyCode +'You pressed the up key'); } if(event.keyCode==39){ alert(event.keyCode +'You pressed the right button'); } if(event.keyCode==40){ alert(event.keyCode +'You pressed the down key'); } } </script> </head> <body onkeydown="keydown(event)"> </body> </html> 4. Common event methods (including window events, mouse events, keyboard events, text events)
5. Event bubbling and event capturingWhen an event occurs, an event stream is generated. When an HTML element generates an event, the event is propagated between the element node and the root node in a specific order, similar to recursion and return. The event triggering method is as follows: addEventListener("click","doSomething","true"); If the third parameter is true, it is event capture, if it is false, it is event bubbling, the default is bubbling. Capture event propagation: Bubbling event propagation This is the end of this article on using javascript to handle common events. For more information about how to handle common events with js, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: MySQL knowledge points for the second-level computer exam mysql alter command
>>: IIS and APACHE implement HTTP redirection to HTTPS
Tomcat is an HTTP server that is the official ref...
Mysql5.7.19 version is a new version launched thi...
The span tag is often used when making HTML web pa...
When you browse many websites, you will find that ...
Table of contents Preface Introduction to Bezier ...
Common usage of Regexp in Mysql Fuzzy matching, c...
CSS to achieve the image hovering mouse folding e...
*** Example of setting the style of a hyperlink a...
Preface When the code runs and an error occurs, w...
Six steps of JDBC: 1. Register the driver 2. Get ...
When associating two tables, a foreign key could ...
I recently bought the cheapest Tencent cloud serv...
The previous article introduced how Vue can reali...
Problem description (what is keep-alive) keep-ali...
Table of contents Preface What is index pushdown?...