国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

詳解Javascript事件驅動編程

2019-11-20 10:52:41
字體:
來源:轉載
供稿:網友

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

二、事件驅動原理

  • 事件源:產生事件的地方(html元素)
  • 事件:點擊/鼠標操作/鍵盤操作等等
  • 事件對象:當某個事件發生時,可能會產生一個事件對象,該時間對象會封裝好該時間的信息,傳遞給事件處理程序
  • 事件處理程序:響應用戶事件的代碼

案例:

<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> 
  • JS事件分類
  • 鼠標事件
  • click dblclick mousedown mouseout mouseover mouseup mousemove等
  • 鍵盤事件
  • keydown keypress keyup等
  • HTML事件
  • window的onload unload error abort 文本框的select change等
  • 其他事件
  • 頁面中有些特殊對象運行過程中產生的事件 

案例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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 怀远县| 当雄县| 庐江县| 大新县| 彰化市| 嘉黎县| 临城县| 云南省| 乌兰察布市| 新泰市| 太白县| 保德县| 公主岭市| 海盐县| 龙游县| 延吉市| 镇江市| 太仓市| 读书| 巧家县| 贞丰县| 龙州县| 陕西省| 通化县| 永川市| 百色市| 汽车| 宁夏| 岳普湖县| 恩平市| 伊宁县| 广昌县| 安阳县| 新兴县| 孝感市| 鞍山市| 鲁甸县| 伊宁市| 襄垣县| 新郑市| 平武县|