整理Javascript事件響應學習筆記
什么是事件
JavaScript 創(chuàng)建動態(tài)頁面。事件是可以被 JavaScript 偵測到的行為。 網(wǎng)頁中的每個元素都可以產(chǎn)生某些可以觸發(fā) JavaScript 函數(shù)或程序的事件。
1、鼠標單擊事件(onclick)
onclick是鼠標單擊事件,當在網(wǎng)頁上單擊鼠標時,就會發(fā)生該事件。同時onclick事件調用的程序塊就會被執(zhí)行,通常與按鈕一起使用。
例:我們單擊按鈕時,觸發(fā) onclick 事件,并調用兩個數(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="點擊提交" onclick="add2()" />
</form>
</body>
</html>
注意: 在網(wǎng)頁中,如使用事件,就在該元素中設置事件屬性。
2、鼠標經(jīng)過事件(onmouseover)
鼠標經(jīng)過事件,當鼠標移到一個對象上時,該對象就觸發(fā)onmouseover事件,并執(zhí)行onmouseover事件調用的程序。
當鼠標經(jīng)過"確定"按鈕后,調用message()函數(shù),彈出消息對話框。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> 鼠標經(jīng)過事件 </title>
<script type="text/javascript">
function message(){
confirm("請輸入密碼后,再單擊確定!"); }
</script>
</head>
<body>
<form>
密碼:
<input name="password" type="password" >
<input name="確定" type="button" value="確定" onmouseover="message()"/>
</form>
</body>
</html>
3、鼠標移開事件(onmouseout)
鼠標移開事件,當鼠標移開當前對象時,執(zhí)行onmouseout調用的程序。
當鼠標移開"點擊我"的按鈕時,調用message()函數(shù),彈出消息對話框。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>鼠標移開事件 </title>
<script type="text/javascript">
function message(){
alert("不要移開,點擊后進行慕課網(wǎng)!"); }
</script>
</head>
<body>
<form>
<a onmouseout="message()">點擊我</a>
</form>
</body>
</html>
4、光標聚焦事件(onfocus)
當網(wǎng)頁中的對象獲得聚點時,執(zhí)行onfocus調用的程序就會被執(zhí)行。
當下拉列表得到焦點時,調用message()函數(shù),就彈出對話框“"請選擇,您現(xiàn)在的職業(yè)!”。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> 光標聚焦事件 </title>
<script type="text/javascript">
function message(){
alert("請選擇,您現(xiàn)在的職業(yè)!");
}
</script>
</head>
<body>
請選擇您的職業(yè):<br>
<form>
<select name="career" onfocus="message()">
<option>學生</option>
<option>教師</option>
<option>工程師</option>
<option>演員</option>
<option>會計</option>
</select>
</form>
</body>
</html>
5、失焦事件(onblur)
onblur事件與onfocus是相對事件,當光標離開當前獲得聚焦對象的時候,觸發(fā)onblur事件,同時執(zhí)行被調用的程序。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> 失焦事件 </title>
<script type="text/javascript">
function message(){
alert("請確定已輸入密碼后,在移開!"); }
</script>
</head>
<body>
<form>
用戶:
<input name="username" type="text" value="請輸入用戶名!" >
密碼:
<input name="password" type="text" value="請輸入密碼!" onblur="message()">
</form>
</body>
</html>
6、內(nèi)容選中事件(onselect)
選中事件,當文本框或者文本域中的文字被選中時,觸發(fā)onselect事件,同時調用的程序就會被執(zhí)行。
當選中個人簡介文本框中文字時,觸發(fā)onselect事件,并彈出對話框。
<!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>
個人簡介:<br>
<textarea name="summary" cols="60" rows="5" onselect="message()">請寫入個人簡介,不少于200字!</textarea>
</form>
</body>
</html>
7、文本框內(nèi)容改變事件(onchange)
通過改變文本框的內(nèi)容來觸發(fā)onchange事件,同時執(zhí)行被調用的程序。
<!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>
個人簡介:<br>
<textarea name="summary" cols="60" rows="5" onchange="message()">請寫入個人簡介,不少于200字!</textarea>
</form>
</body>
</html>
8、加載事件(onload)
事件會在頁面加載完成后,立即發(fā)生,同時執(zhí)行被調用的程序。
注意: 1. 加載頁面時,觸發(fā)onload事件,事件寫在<body>標簽內(nèi)。
2. 此節(jié)的加載頁面,可理解為打開一個新頁面時。
如下代碼,當加載一個新頁面時,彈出對話框“加載中,請稍等…”。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> 加載事件 </title>
<script type="text/javascript">
function message(){
alert("加載中,請稍等…"); }
</script>
</head>
<body onload="message()">
歡迎學習JavaScript。
</body>
</html>
9、卸載事件(onunload)
當用戶退出頁面時(頁面關閉、頁面刷新等),觸發(fā)onUnload事件,同時執(zhí)行被調用的程序。
注意:不同瀏覽器對onunload事件支持不同。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> 卸載事件 </title>
<script type="text/javascript">
window.onunload = onunload_message;
function onunload_message(){
alert("您確定離開該網(wǎng)頁嗎?");
}
</script>
</head>
<body onunload="onunload_message">
歡迎學習JavaScript。
</body>
</html>
測試結果發(fā)現(xiàn)只有在IE瀏覽器里執(zhí)行,其他瀏覽器不執(zhí)行。
以上就是關于Javascript事件響應的九種狀態(tài),希望對大家的學習有所幫助。
相關文章
單元測試框架Jest搭配TypeScript的安裝與配置方式
這篇文章主要介紹了單元測試框架Jest搭配TypeScript的安裝與配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01

