Events - General
-
Concept: some components are executed some operations, trigger the execution of some code.
-
Event: some actions. For example: click, double-click, keyboard pressed, mouse moved
-
Event source: component. Such as: button, text input box, etc
-
Listener: code.
-
Register listening: combine event, event source and listener together. When an event occurs on the event source, the execution of a listener code is triggered.
-
-
Common events:
-
Click event:
-
onclick: click event
-
ondblclick: double click event
-
-
Focus events
-
onblur: losing focus
-
onfocus: element gets focus.
-
-
Load event:
-
onload: a page or an image is loaded.
-
-
Mouse events:
-
onmousedown mouse button is pressed.
-
onmouseup the mouse button is released.
-
onmousemove mouse is moved.
-
onmouseover mouse over an element.
-
onmouseout mouse moves away from an element.
-
-
Keyboard events:
-
Onkeydowna keyboard key is pressed.
-
Onkeyupa keyboard key is released.
-
onkeypress a keyboard key is pressed and released.
-
-
Selection and change
-
The content of onchang E domain has been changed.
-
The onselect text is selected.
-
-
Form event:
-
The onsubmit confirmation button is clicked.
-
The onreset button is clicked.
-
-
Case 5 - select all forms
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Form selection</title> <style> table{ border: 1px solid; width: 500px; margin-left: 30%; } td,th{ text-align: center; border: 1px solid; } div{ margin-top: 10px; margin-left: 30%; } .out{ background-color: white; } .over{ background-color: pink; } </style> </head> <body> <table> <caption>Student information form</caption> <tr> <th><input type="checkbox" name="cb" id="fist_cb"></th> <th>number</th> <th>Full name</th> <th>Gender</th> <th>operation</th> </tr> <tr> <td><input type="checkbox" name="cb"></td> <td>1</td> <td>Linghu Chong</td> <td>male</td> <td><a href="javascript:void(0);">delete</a></td> </tr> <tr> <td><input type="checkbox" name="cb"></td> <td>2</td> <td>Ren Woxing</td> <td>male</td> <td><a href="javascript:void(0);">delete</a></td> </tr> <tr> <td><input type="checkbox" name="cb"></td> <td>3</td> <td>Yue buqun</td> <td>?</td> <td><a href="javascript:void(0);">delete</a></td> </tr> </table> <div> <input type="button" id="selectAll" value="All election"> <input type="button" id="unSelectAll" value="Totally unselected"> <input type="button" id="selectRev" value="Reverse election"> </div> <script> //All election document.getElementById("selectAll").onclick = function () { //Get all var cbs = document.getElementsByName("cb"); for (var i = 0; i < cbs.length; i++) { //Set each property to checked cbs[i].checked = true; } } //Totally unselected document.getElementById("unSelectAll").onclick = function () { //Get all var cbs = document.getElementsByName("cb"); for (var i = 0; i < cbs.length; i++) { //Set each property to checked cbs[i].checked = false; } } //Reverse election document.getElementById("selectRev").onclick = function () { //Get all var cbs = document.getElementsByName("cb"); for (var i = 0; i < cbs.length; i++) { //Set each property to checked cbs[i].checked = !cbs[i].checked; } } //Set other buttons to the same state as the first button document.getElementById("fist_cb").onclick = function () { //Get all var cbs = document.getElementsByName("cb"); for (var i = 0; i < cbs.length; i++) { //Set each property to checked this as the first cbs[i].checked = this.checked; } } //Set the background color to change when the mouse moves up var trs = document.getElementsByTagName("tr"); //ergodic for (var i = 0; i < trs.length; i++) { //Move to element trs[i].onmouseover = function () { this.className = "over"; } //Removed elements trs[i].onmouseout = function () { this.className = "out"; } } </script> </body> </html>
Case 6 - form inspection
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Registration page</title> <style> *{ margin: 0px; padding: 0px; box-sizing: border-box; } body{ background: url("img/register_bg.png") no-repeat center; padding-top: 25px; } .rg_layout{ width: 900px; height: 500px; border: 8px solid #EEEEEE; background-color: white; /*Center the div horizontally*/ margin: auto; } .rg_left{ /*border: 1px solid red;*/ float: left; margin: 15px; } .rg_left > p:first-child{ color:#FFD026; font-size: 20px; } .rg_left > p:last-child{ color:#A6A6A6; font-size: 20px; } .rg_center{ float: left; /* border: 1px solid red;*/ } .rg_right{ /*border: 1px solid red;*/ float: right; margin: 15px; } .rg_right > p:first-child{ font-size: 15px; } .rg_right p a { color:pink; } .td_left{ width: 100px; text-align: right; height: 45px; } .td_right{ padding-left: 50px ; } #username,#password,#email,#name,#tel,#birthday,#checkcode{ width: 251px; height: 32px; border: 1px solid #A6A6A6 ; /*Set border fillet*/ border-radius: 5px; padding-left: 10px; } #checkcode{ width: 110px; } #img_check{ height: 32px; vertical-align: middle; } #btn_sub{ width: 150px; height: 40px; background-color: #FFD026; border: 1px solid #FFD026 ; } #td_sub{ padding-left: 150px; } .error{ color:red; vertical-align: middle; } </style> <script> window.onload = function () { document.getElementById("form").onsubmit = function () { //Verify user name //Verify password //Returns true if all are successful return checkUsername() && checkPassword(); } document.getElementById("username").onblur = checkUsername; document.getElementById("password").onblur = checkPassword; } function checkUsername() { //Get the contents of the input box var username = document.getElementById("username").value; //Define regular var reg_username = /^\w{6,12}$/; //Check whether it is regular var flag = reg_username.test(username); if(flag){ s_username.innerHTML = "<img height='25' width='35' src='img/gou.png'>" }else{ s_username.innerHTML = "Incorrect format of user name"; } return flag; } function checkPassword() { //Get the contents of the input box var username = document.getElementById("password").value; //Define regular var reg_username = /^\w{6,12}$/; //Check whether it is regular var flag = reg_username.test(username); if(flag){ s_password.innerHTML = "<img height='25' width='35' src='img/gou.png'>" }else{ s_password.innerHTML = "Incorrect password format"; } return flag; } </script> </head> <body> <div class="rg_layout"> <div class="rg_left"> <p>New user registration</p> <p>USER REGISTER</p> </div> <div class="rg_center"> <div class="rg_form"> <!--Define form form--> <form action="#" id="form" method="get"> <table> <tr> <td class="td_left"><label for="username">User name</label></td> <td class="td_right"> <input type="text" name="username" id="username" placeholder="enter one user name"> <span id="s_username" class="error"></span> </td> </tr> <tr> <td class="td_left"><label for="password">Password</label></td> <td class="td_right"> <input type="password" name="password" id="password" placeholder="Please input a password"> <span id="s_password" class="error"></span> </td> </tr> <tr> <td class="td_left"><label for="email">Email</label></td> <td class="td_right"><input type="email" name="email" id="email" placeholder="Please enter email"></td> </tr> <tr> <td class="td_left"><label for="name">Full name</label></td> <td class="td_right"><input type="text" name="name" id="name" placeholder="Please enter a name"></td> </tr> <tr> <td class="td_left"><label for="tel">Cell-phone number</label></td> <td class="td_right"><input type="text" name="tel" id="tel" placeholder="Please enter your mobile number"></td> </tr> <tr> <td class="td_left"><label>Gender</label></td> <td class="td_right"> <input type="radio" name="gender" value="male"> male <input type="radio" name="gender" value="female"> female </td> </tr> <tr> <td class="td_left"><label for="birthday">Date of birth</label></td> <td class="td_right"><input type="date" name="birthday" id="birthday" placeholder="Please enter the date of birth"></td> </tr> <tr> <td class="td_left"><label for="checkcode" >Verification Code</label></td> <td class="td_right"><input type="text" name="checkcode" id="checkcode" placeholder="Please enter the verification code"> <img id="img_check" src="img/verify_code.jpg"> </td> </tr> <tr> <td colspan="2" id="td_sub"><input type="submit" id="btn_sub" value="register"></td> </tr> </table> </form> </div> </div> <div class="rg_right"> <p>Existing accounts?<a href="#"> log in now</a></p> </div> </div> </body> </html>