一、基本概述
JS是采用事件驅動的機制來響應用戶操作的,也就是說當用戶對某個html元素進行操作的時候,會產生一個時間,該時間會驅動某些函數來處理。
PS:這種方式和Java GUI中的事件監聽機制很像,都是需要注冊監聽,然后再處理監聽,只不過實現的方式不同而已。
二、事件驅動原理

案例:
<html> <head> <script type="text/javascript"> function test1(e){ window.alert("x=" + e.clientX + " y=" + e.clientY); } function test2(e){ window.alert("x=" + e.clientX + " y=" + e.clientY); } function test3(e){ window.alert(new Date().toLocaleString()); } function test4(e){ if(e.value == "red"){ div1.style.backgroundColor = "red"; } else if (e.value == "black"){ div1.style.backgroundColor = "black"; } } </script> </head> <body> <input type="button" onclick="test1(event)" value="button1"> <input type="button" onmouseover="test2(event)" value="button2"> <input type="button" onclick="test3(event)" value="button3"> <div id="div1" style="width: 400px; height: 300px; background-color: red"></div> <input type="button" onclick="test4(this)" value="red"> <input type="button" onclick="test4(this)" value="black"> </body> </html> 案例1:監聽鼠標點擊事件,并能夠顯示鼠標點擊的位置x,y
<html> <head> <script> function test1(e){ window.alert("x="+e.clientX+"y="+e.clientY); } </script> </head> <body onmousedown="test1(event)"> </body> </html> 點擊瀏覽器之后,顯示坐標(有些瀏覽器可能無效)
案例2:點擊按鈕,圖片變成紅色,黑色
方法:JS訪問內部css
//js如何訪問css屬性,來改變外觀 <html> <head> <script> function test3(e){ var pic=document.getElementById("pic"); if(e.value=="紅色"){ pic.style.backgroundColor="red"; } else if(e.value=="黑色"){ pic.style.backgroundColor="black"; } } </script> </head> <body > <div id="pic" style="border:1;background-color:red;width:300px;height:300px"></div> <input type="button" onclick="test3(this)" value="紅色"> <input type="button" onclick="test3(this)" value="黑色"> </body> </html> 方法:JS訪問外部css(這方法不一定適用于所有瀏覽器)
event2.css.style { border:1; background-color:red; width:300px; height:300px; } event2.html<html> <head> <script> function test3(e){ //取連接的第一個css文件的內容用0 var ocssRules=document.styleSheets[0].rules; //從ocssRules取出你希望的樣式 var style=ocssRules[0];//這里面的0表示event2.css文件中第一個規則 if(e.value=="黑色"){ style.style.backgroundColor="black"; } else if(e.value=="紅色"){ style.style.backgroundColor="red"; } } </script> </head> <body> <div class="style"></div> <input type="button" onclick="test3(this)" value="紅色"> <input type="button" onclick="test3(this)" value="黑色"> </body> </html> 案例3:區分當前瀏覽器的內核是什么?(區分出ie6/7/8/ 火狐等)
<script language="javascript"> if(window.XMLHttpRequest) { //Mozilla, Safari, IE7,IE8 if(!window.ActiveXObject) { // Mozilla, Safari, alert('Mozilla, Safari'); } else { alert('IE7 .8'); } } else { alert('IE6'); } </script> 案例4:一個事件可以被多個函數監聽
<html> <head> function test(e){ window.alert("fss"); } function test1(e){ window.alert("sfdsdf"); } </script> </head> <body> <div class="style"></div> <input type="button" onclick="test(this),test1(this)" value="紅色"> </body> </html> 案例5:防止用戶通過點擊鼠標右鍵菜單拷貝網頁內容,選擇網頁內容
<html> <script type="text/javascript"> function test(){ //window.alert("沒有菜單"); return false; } function test2(){ //window.alert("全選不行"); return false; } </script> </head> <!--body元素響應oncontextmenu,onselectstart事件 --> <body oncontextmenu="return test()" onselectstart="return test2()"> 內容 </body> </html> 下篇文章為大家分享一個簡單綜合案例:簡單的計算器,希望大家不要錯過。
關于Javascript事件驅動編程遠不止這些,希望本文所述對大家學習javascript程序設計有所幫助。
新聞熱點
疑難解答