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
Table of contents 1. Determine the entity type be...
1. Command Introduction The userdel (user delete)...
Table of contents Case 1: Case 2: Case 3: To summ...
I have never used apache. After I started working...
Prerequisite: Percona 5.6 version, transaction is...
The backend uses the thinkphp3.2.3 framework. If ...
Table of contents Preface keep-avlive hook functi...
By default, /etc/default/docker configuration wil...
1. scroll-view When using vertical scrolling, you...
VMWare (Virtual Machine ware) is a "virtual ...
Table of contents 1- Error details 2-Single Solut...
Table of contents 1. The concept of filter 1. Cus...
Reasonable setting of MySQL sql_mode sql_mode is ...
CSS style specifications 1. Class Selector 2. Tag...
Copy code The code is as follows: <body <fo...