javascript學習筆記(五):異常捕獲和事件處理

來源:互聯網
上載者:User

標籤:val   處理   sel   點擊   nbsp   樣式   ref   學習筆記   css   

異常捕獲

Try{

  發生異常的代碼塊

}catch(err){

  異常資訊處理

}

 1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4     <meta chaset="UTF-8"> 5     <title></title>     6 </head>     7 <body> 8     <form> 9         <input id="txt" type="text">10         <input id="btn" type="button" onclick="demo()" value="按鈕">11     </form>12     <script>13         function demo(){14             try{15                 var e = document.getElementById("txt").value;16                 if(e==""){17                     throw "請輸入";   //一般throw會與try,catch配合使用18                 }19             }catch(err){20                 alert(err);21             }22         }23     </script>24 </body>25 </html>

 

事件處理

1、onclick滑鼠點擊事件

2、onmouseout滑鼠離開事件

3、onmouseover滑鼠經過事件

4、onchange文字框內容改變事件

5、onselect文字框內容選中事件

6、onfocus游標聚集事件

7、onload網頁載入完畢事件

 1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4     <meta chaset="UTF-8"> 5     <title></title>     6     <link rel="stylesheet" type="text/css" href="style.css"> <!--指定rel為樣式表,類型為css,外部檔案為style.css--> 7 </head>     8 <body onload="onLoad()">                       <!--onload網頁載入完畢事件--> 9     <button onclick="onClick()">按鈕</button>           <!--onclick滑鼠點擊事件-->10     <div class="div" onmouseout="onOut(this)" onmouseover="onOver(this)"></div> <!--onmouseout滑鼠離開事件,onmouseover滑鼠經過事件 -->11     <form>12         <input type="text" onchange="onChange(this)">     <!--onchange文字框內容改變事件-->13         <br>14         <input type="text" onselect="onSelect(this)" onfocus="onFocus(this)">   <!--onselect文字框內容選中事件,onfocus游標聚集事件-->15     </form>    16     <script>17         function onClick(){18             alert("onclick滑鼠點擊");19         }20         function onOver(ooj){21             ooj.innerHTML="onmouseover滑鼠經過";22         }23         function onOut(ooj){24             ooj.innerHTML="onmouseout滑鼠離開";25         }    26         function onChange(bg){27             alert("onchange文字框內容改變");28         }29         function onSelect(bg){30             bg.style.background="yellow";31             alert("onselect文字框內容選中");32         }33         function onFocus(bg){34             bg.style.background="green";35             alert("onfocus游標聚集");36         }37         function onLoad(){38             alert("onload網頁載入完畢");39         }40     </script>41 </body>42 </html>

 

javascript學習筆記(五):異常捕獲和事件處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.