JavaScript HTML DOM events
JavaScript HTML DOM Events
HTML DOM gives JavaScript the ability to react to HTML events.
React to events
We can execute JavaScript when an event occurs, such as when the user clicks on an HTML element.
To execute code when the user clicks an element, add JavaScript code to an HTML event attribute:
onclick=JavaScript
Example of HTML event:
When the user clicks the mouse When the web page is loaded When the image is loaded When the mouse moves over the element When the input field is changed When the HTML form is submitted When the user triggers a key
In this In this example, when the user clicks, the content of the <h1> element will be changed:
<!doctype html> <html> <meta charset="utf-8"> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus®"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <title>Document</title> </head> <body> <h1 onclick="this.innerHTML='hello!'">点我点我点我!</h1> </body> </html>
In this example, the function will be called from the event handler:
<!DOCTYPE html> <html> <meta charset="utf-8"> <head> <script> function changetext(id) { id.innerHTML="hello!"; } </script> </head> <body> <h1 onclick="changetext(this)">请点击这段文本!</h1> </body> </html>
HTML Event Attribute
To assign an event to an HTML element, you can use the event attribute.
Assign an onclick event to the button element:
<!DOCTYPE html> <html> <meta charset="utf-8"> <body> <p>点击按钮来执行 <b>displayDate()</b> 函数。</p> <button onclick="displayDate()">试一试</button> <script> function displayDate() { document.getElementById("demo").innerHTML=Date(); } </script> <p id="demo"></p> </body> </html>
In the above example, when the button is clicked, the function named displayDate will be executed.
Using HTML DOM to assign events
HTML DOM allows you to use JavaScript to assign events to HTML elements:
Assign onclick events to button elements:
<!DOCTYPE html> <html> <meta charset="utf-8"> <head> </head> <body> <p>点击按钮来执行 <b>displayDate()</b> 函数。</p> <button id="myBtn">试一试</button> <script> document.getElementById("myBtn").onclick=function(){displayDate()}; function displayDate() { document.getElementById("demo").innerHTML=Date(); } </script> <p id="demo"></p> </body> </html>
In the above example, the function named displayDate is assigned to the HTML element with id=myButn".
When the button is clicked, the function will be executed.
onload and onunload events
The onload and onunload events are triggered when a user enters or leaves the page.
The onload event can be used to check the visitor's browser type and version so that different versions of the web page can be loaded based on this information.
The onload and onunload events can be used to handle cookies.
<!DOCTYPE html> <html> <meta charset="utf-8"> <body onload="checkCookies()"> <script> function checkCookies() { if (navigator.cookieEnabled==true) { alert("Cookies are enabled") } else { alert("Cookies are not enabled") } } </script> <p>弹出的提示框会告诉你浏览器是否已启用 cookie。</p> </body> </html>
onchange event
The onchange event is often used for input field validation.
The following example shows. How to use onchange. The upperCase() function is called when the user changes the content of the input field.
<!DOCTYPE html> <html> <meta charset="utf-8"> <head> <script> function myFunction() { var x=document.getElementById("fname"); x.value=x.value.toUpperCase(); } </script> </head> <body> 请输入你的英文名:<input type="text" id="fname" onchange="myFunction()"> <p>当你离开输入框时,被触发的函数会把你输入的文本转换为大写字母。</p> </body> </html>
onmouseover and onmouseout events
onmouseover and onmouseout events can be used when the mouse pointer moves to or away. Trigger function when element.
A simple onmouseover-onmouseout example:
<!DOCTYPE html> <html> <meta charset="utf-8"> <body> <div onmouseover="mOver(this)" onmouseout="mOut(this)" style="background-color:#D94A38;width:200px;height:50px;padding-top:25px;text-align:center;"> Mouse Over Me </div> <script> function mOver(obj) { obj.innerHTML="谢谢你" } function mOut(obj) { obj.innerHTML="把鼠标指针移动到上面" } </script> </body> </html>
onmousedown, onmouseup and onclick events
Onmousedown, onmouseup and onclick events are the entire process of mouse click. First, when a mouse button is clicked, the onmousedown event is triggered. Then, when the mouse button is released, the onmouseup event is triggered. Finally, when the mouse click is completed, the onclick event is triggered.
A simple onmousedown-onmouseup example:
<!DOCTYPE html> <html> <meta charset="utf-8"> <body> <div onmousedown="mDown(this)" onmouseup="mUp(this)" style="background-color:#D94A38;width:200px;height:50px;padding-top:25px;text-align:center;"> 点击这里 </div> <script> function mDown(obj) { obj.style.backgroundColor="#1ec5e5"; obj.innerHTML="松开鼠标" } function mUp(obj) { obj.style.backgroundColor="#D94A38"; obj.innerHTML="谢谢你" } </script> </body> </html>