Javascript basic tutorial events
Let’s look at the following common events
onchange change event
Instance, code As shown below, when lowercase letters are entered in the text box, the mouse leaves the text box and changes to uppercase letters
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>事件</title> <script type="text/javascript"> function cha(){ var x=document.getElementById("name"); x.value=x.value.toUpperCase(); } </script> </head> <body> 请输入:<input type="text" id="name" onchange="cha()"> </body> </html>
onclick click event
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>事件</title> <script type="text/javascript"> function cl(){ alert("php中文网"); } </script> </head> <body> <input type="button" value="点击" onclick="cl()"> </body> </html>
onmouseover and onmouseout events
Instance, let’s make an example, put the mouse on it, it will appear, welcome When the mouse leaves the php Chinese website, the text becomes php Chinese website
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>事件</title> <style type="text/css"> #dv{ width:200px;height:200px;border:1px solid #f60;background-color: red; text-align: center;line-height:200px; } </style> </head> <body> <div id="dv" onmouseover="over(this)" onmouseout="out(this)"></div> <script type="text/javascript"> function over(obj){ obj.innerHTML = "欢迎来到php中文网"; //鼠标放上去时 } function out(obj){ obj.innerHTML = "php中文网"; // 鼠标离开后 } </script> </body> </html>
##onkeydown keyboard event
Now let’s do an example. In the text box, we press the keyboard and a prompt window will pop up<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>事件</title> <script type="text/javascript"> function onkey(){ alert("您已触发键盘事件"); } </script> </head> <body> 键盘事件:<input type="text" onkeydown="onkey()"> </body> </html>
onload Loading event
Let’s write an example below:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>事件</title> <script type="text/javascript"> function checkCookies(){ if (navigator.cookieEnabled==true){ alert("Cookies 可用") }else{ alert("Cookies 不可用") } } </script> </head> <body onload="checkCookies()"> php 中文网 </body> </html>