關於JavaScript事件與函數
阿新 • • 發佈:2018-05-10
enter pass onsubmit 函數 hid span option als obj
如果事件是在特定條件下觸發的行為,那麽函數就是實現特定功能的行為的具體的體現。
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />表單事件示例5 <title>表單事件示例</title> 6 <style type="text/css"> 7 div input,select{ 8 width:130px;} 9 </style> 10 <script type="text/javascript"> 11 /*得到焦點*/ 12 function textFocus(obj){ 13 obj.style.background="#33f";} 14 /*失去焦點*/ 15 function textblur(obj){16 obj.style.background="#ff5";} 17 /*數據驗證*/ 18 function verifySubmit(){ 19 if(document.getElementById("name").value==""){ 20 alert("姓名不能為空"); 21 document.getElementById("name").focus(); 22 return false; 23 } 24 if(document.getElementById("pwd").value==""){ 25 alert("密碼不能為空"); 26 document.getElementById("pwd").focus(); 27 return false; 28 } 29 if(document.getElementById("sex").value==""){ 30 alert("請選擇性別"); 31 document.getElementById("sex").focus(); 32 return false; 33 } 34 if(document.getElementById("email").value==""){ 35 alert("請填寫email"); 36 document.getElementById("email").focus(); 37 return false; 38 } 39 return true; 40 } 41 </script> 42 </head> 43 <body style="font-size:14px"> 44 <form name="form" onsubmit="return verifySubmit()"> 45 <div> 46 <table bordercolor="#33FFFF"> 47 <tr> 48 <td>姓 名:</td> 49 <td><input type="text" id="name" onfocus="textFocus(this)" onblur="textblur(this)"/></td> 50 </tr> 51 <tr> 52 <td>密 碼:</td> 53 <td><input type="password" id="pwd" onfocus="textFocus(this)" onblur="textblur(this)"/></td> 54 </tr> 55 <td>性 別:</td> 56 <td> 57 <select id="sex"> 58 <option value="">--------請選擇--------</option> 59 <option value="M">男</option> 60 <option value="F">女</option> 61 </select></td> 62 </tr> 63 <tr> 64 <td>郵 箱:</td> 65 <td><input type="text" id="email" onfocus="textFocus(this)" onblur="textblur(this)"/></td> 66 </tr> 67 </tr> 68 <tr> 69 <td align="right"></td> 70 <td align="center"><input type="submit" value="提 交"/><input type="reset" value="重 置"/></td> 71 </tr> 72 </table> 73 </div> 74 </form> 75 </body> 76 </html>
isNaN() | 檢查某個值是否是數字 |
parseFloat() | 解析字符串並返回一個浮點數 |
ecodeURI | 將字符串編碼為URI |
decodeURI | 解碼某編碼的URI |
unecape() | 對escap()編碼的字符串進行解碼 |
escap() | 對字符串進行編碼 |
parseInt() | 解析字符串並返回整數 |
eval() | 將字符串當做表達式執行 |
關於JavaScript事件與函數