六月伊人,国产精品制服丝袜欧美,亚洲va在线∨a天堂va欧美va,精品亚洲一区二区三区在线观看国产老熟女色视频,国产熟女九色,国产又粗又大,久久人人网国产精品

JS基本事件總結(jié)

2018-3-25    seo達(dá)人

如果您想訂閱本博客內(nèi)容,每天自動(dòng)發(fā)到您的郵箱中, 請(qǐng)點(diǎn)這里

概述

JavaScript 創(chuàng)建動(dòng)態(tài)頁(yè)面。事件是可以被 JavaScript 偵測(cè)到的行為。 網(wǎng)頁(yè)中的每個(gè)元素都可以產(chǎn)生某些可以觸發(fā) JavaScript 函數(shù)或程序的事件。比如說,當(dāng)用戶單擊按鈕或者提交表單數(shù)據(jù)時(shí),就發(fā)生一個(gè)鼠標(biāo)單擊(onclick)事件,需要瀏覽器做出處理,返回給用戶一個(gè)結(jié)果。主要事件表總結(jié)如下: 
JS事件


鼠標(biāo)單擊事件( onclick )

onclick是鼠標(biāo)單擊事件,當(dāng)在網(wǎng)頁(yè)上單擊鼠標(biāo)時(shí),就會(huì)發(fā)生該事件。同時(shí)onclick事件調(diào)用的程序塊就會(huì)被執(zhí)行,通常與按鈕一起使用。 
單擊按鈕時(shí),觸發(fā) onclick 事件,并調(diào)用兩個(gè)數(shù)和的函數(shù)add2()。代碼如下:

<html> <head> <script type="text/javascript"> function add2(){ var numa,numb,sum;
        numa=6;
        numb=8;
        sum=numa+numb;
        document.write("兩數(shù)和為:"+sum);  } </script> </head> <body> <form> <input name="button" type="button" value="點(diǎn)擊提交" onclick="add2()" /> </form> </body> </html>
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

注意: 在網(wǎng)頁(yè)中,如使用事件,就在該元素中設(shè)置事件屬性。


鼠標(biāo)經(jīng)過事件(onmouseover)

鼠標(biāo)經(jīng)過事件,當(dāng)鼠標(biāo)移到一個(gè)對(duì)象上時(shí),該對(duì)象就觸發(fā)onmouseover事件,并執(zhí)行onmouseover事件調(diào)用的程序。鼠標(biāo)經(jīng)過”確定”按鈕時(shí),觸發(fā)onmouseover事件,調(diào)用函數(shù)message(),彈出消息框,代碼如下:

<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title> 鼠標(biāo)經(jīng)過事件 </title> <script type="text/javascript"> function message(){ confirm("請(qǐng)輸入密碼后,再單擊確定!"); } </script> </head> <body> <form> 密碼:<input name="password" type="password" > <input name="確定" type="button" value="確定" onmouseover="message();"/> </form> </body> </html>
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

運(yùn)行結(jié)果: 
2


鼠標(biāo)移開事件(onmouseout)

鼠標(biāo)移開事件,當(dāng)鼠標(biāo)移開當(dāng)前對(duì)象時(shí),執(zhí)行onmouseout調(diào)用的程序。當(dāng)把鼠標(biāo)移動(dòng)到”登錄”按鈕上,然后再移開時(shí),觸發(fā)onmouseout事件,調(diào)用函數(shù)message(),代碼如下:

<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>鼠標(biāo)移開事件 </title> <script type="text/javascript"> function message(){ alert("百度一下,你就知道!"); } </script> </head> <body> <form> <a href="http://www.baidu.com" onmouseout="message()">點(diǎn)擊我</a> </form> </body> </html>
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

運(yùn)行結(jié)果: 
3


光標(biāo)聚焦事件(onfocus)

當(dāng)網(wǎng)頁(yè)中的對(duì)象獲得聚點(diǎn)時(shí),執(zhí)行onfocus調(diào)用的程序就會(huì)被執(zhí)行。 
如下代碼, 當(dāng)將光標(biāo)移到文本框內(nèi)時(shí),即焦點(diǎn)在文本框內(nèi),觸發(fā)onfocus 事件,并調(diào)用函數(shù)message()。

<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title> 光標(biāo)聚焦事件 </title> <script type="text/javascript"> function message(){ alert("請(qǐng)選擇,您現(xiàn)在的職業(yè)!");
    } </script> </head> <body> <form onfocus="message()"> <input type="text" onfocus="message()">輸入</input> </form> </body> </html>
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

運(yùn)行結(jié)果: 
4


失焦事件(onblur)

onblur事件與onfocus是相對(duì)事件,當(dāng)光標(biāo)離開當(dāng)前獲得聚焦對(duì)象的時(shí)候,觸發(fā)onblur事件,同時(shí)執(zhí)行被調(diào)用的程序。如下代碼, 網(wǎng)頁(yè)中有用戶和密碼兩個(gè)文本框。當(dāng)前光標(biāo)在用戶文本框內(nèi)時(shí)(即焦點(diǎn)在文本框),在光標(biāo)離開該文本框后(即失焦時(shí)),觸發(fā)onblur事件,并調(diào)用函數(shù)message()。

<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title> 失焦事件 </title> <script type="text/javascript"> function message(){ alert("請(qǐng)記得輸入密碼!"); } </script> </head> <body> <form> 用戶:<input name="username" type="text" value="請(qǐng)輸入用戶名!" onblur="message()" > 密碼:<input name="password" type="text" value="請(qǐng)輸入密碼!" > </form> </body> </html>
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

運(yùn)行結(jié)果: 
5


內(nèi)容選中事件(onselect)

選中事件,當(dāng)文本框或者文本域中的文字被選中時(shí),觸發(fā)onselect事件,同時(shí)調(diào)用的程序就會(huì)被執(zhí)行。如下代碼,當(dāng)選中用戶文本框內(nèi)的文字時(shí),觸發(fā)onselect 事件,并調(diào)用函數(shù)message()。

<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title> 內(nèi)容選中事件 </title> <script type="text/javascript"> function message(){ alert("您觸發(fā)了選中事件!"); } </script> </head> <body> <form> 個(gè)人簡(jiǎn)介:<br> <textarea name="summary" cols="60" rows="5" onselect="message()">請(qǐng)寫入個(gè)人簡(jiǎn)介,不少于200字!</textarea> </form> </body> </html>
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

運(yùn)行結(jié)果: 
321


文本框內(nèi)容改變事件(onchange)

通過改變文本框的內(nèi)容來觸發(fā)onchange事件,同時(shí)執(zhí)行被調(diào)用的程序。 
如下代碼,當(dāng)用戶將文本框內(nèi)的文字改變后,彈出對(duì)話框“您改變了文本內(nèi)容!”

<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title> 文本框內(nèi)容改變事件 </title> <script type="text/javascript"> function message(){ alert("您改變了文本內(nèi)容!"); } </script> </head> <body> <form> 個(gè)人簡(jiǎn)介:<br> <textarea name="summary" cols="60" rows="5" onchange="message()">請(qǐng)寫入個(gè)人簡(jiǎn)介,不少于200字!</textarea> </form> </body> </html>
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

運(yùn)行結(jié)果: 
2321


加載事件(onload)

事件會(huì)在頁(yè)面加載完成后,立即發(fā)生,同時(shí)執(zhí)行被調(diào)用的程序。注意:

  • 加載頁(yè)面時(shí),觸發(fā)onload事件,事件寫在標(biāo)簽內(nèi)。
  • 此節(jié)的加載頁(yè)面,可理解為打開一個(gè)新頁(yè)面時(shí)。

如下代碼,當(dāng)加載一個(gè)新頁(yè)面時(shí),彈出對(duì)話框“加載中,請(qǐng)稍等…”。

<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title> 加載事件 </title> <script type="text/javascript"> function message(){ alert("加載中,請(qǐng)稍等…"); } </script> </head> <body onload="message()"> 歡迎學(xué)習(xí)JavaScript。 </body> </html>
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

運(yùn)行結(jié)果: 
432


卸載事件(onunload)

當(dāng)用戶退出頁(yè)面時(shí)(頁(yè)面關(guān)閉、頁(yè)面刷新等),觸發(fā)onUnload事件,同時(shí)執(zhí)行被調(diào)用的程序。注意:不同瀏覽器對(duì)onunload事件支持不同。如下代碼,當(dāng)退出頁(yè)面時(shí),彈出對(duì)話框“您確定離開該網(wǎng)頁(yè)嗎?”。

<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title> 卸載事件 </title> <script type="text/javascript"> window.onunload = message; function message(){ alert("您確定離開該網(wǎng)頁(yè)嗎?");   
    } </script> </head> <body onunload="message()"> 歡迎學(xué)習(xí)JavaScript。 </body> </html>
藍(lán)藍(lán)設(shè)計(jì)m.wnxcall.com )是一家專注而深入的界面設(shè)計(jì)公司,為期望卓越的國(guó)內(nèi)外企業(yè)提供卓越的UI界面設(shè)計(jì)、BS界面設(shè)計(jì) 、 cs界面設(shè)計(jì) 、 ipad界面設(shè)計(jì) 、 包裝設(shè)計(jì) 、 圖標(biāo)定制 、 用戶體驗(yàn) 、交互設(shè)計(jì)、 網(wǎng)站建設(shè) 平面設(shè)計(jì)服務(wù)


分享本文至:

日歷

鏈接

個(gè)人資料

存檔