html頁面中event的常見應用

來源:互聯網
上載者:User

標籤:style   blog   http   color   java   os   strong   io   

一:擷取鍵盤上某個按鍵的unicode值

 1 <html> 2 <head> 3 <script type="text/javascript"> 4 function whichButton(event) 5 { 6 alert(event.keyCode) 7 } 8  9 </script>10 </head>11 12 <body onkeyup="whichButton(event)">13 <p><b>注釋:</b>在測試這個例子時,要確保右側的架構獲得了焦點。</p>14 <p>在鍵盤上按一個鍵。訊息框會提示出該按鍵的 unicode。</p>15 </body>16 17 </html>
View Code

二:請在頁面內點擊滑鼠的按鍵。判斷你點擊的是哪個鍵

 

 1 <html> 2 <head> 3 <script type="text/javascript"> 4 function whichButton(event) 5 { 6 var btnNum = event.button; 7 if (btnNum==2) 8   { 9   alert("您點擊了滑鼠右鍵!")10   }11 else if(btnNum==0)12   {13   alert("您點擊了滑鼠左鍵!")14   }15 else if(btnNum==1)16   {17   alert("您點擊了滑鼠中鍵!");18   }19 else20   {21   alert("您點擊了" + btnNum+ "號鍵,我不能確定它的名稱。");22   }23 }24 </script>25 </head>26 27 <body onmousedown="whichButton(event)">28 <p>請在文檔中點擊滑鼠。一個訊息框會提示出您點擊了哪個滑鼠按鍵。</p>29 </body>30 31 </html>
View Code

 

三:擷取游標的座標(相對於當前文檔)

 

 1 <html> 2 <head> 3 <script type="text/javascript"> 4 function show_coords(event) 5 { 6 x=event.clientX 7 y=event.clientY 8 alert("X 座標: " + x + ", Y 座標: " + y) 9 }10 </script>11 </head>12 13 <body onmousedown="show_coords(event)">14 15 <p>請在文檔中點擊。一個訊息框會提示出滑鼠指標的 x 和 y 座標。</p>16 17 </body>18 </html>
View Code

 

四:相對於螢幕游標的座標

 

 1 <html> 2 <head> 3  4 <script type="text/javascript"> 5 function coordinates(event) 6 { 7 x=event.screenX 8 y=event.screenY 9 alert("X=" + x + " Y=" + y)10 }11 12 </script>13 </head>14 <body onmousedown="coordinates(event)">15 16 <p>17 在文檔中點擊某個位置。訊息框會提示出指標相對於螢幕的 x 和 y 座標。18 </p>19 20 </body>21 </html>
View Code

 

五:擷取事件觸發的類型

 

 1 <html> 2 <head> 3 <script type="text/javascript"> 4 function getEventType(event) 5   {  6   alert(event.type); 7   } 8 </script> 9 </head>10 11 <body onmousedown="getEventType(event)">12 13 <p>在文檔中點擊某個位置。訊息框會提示出被觸發的事件的類型。</p>14 15 </body>16 </html>
View Code

 

六:當輸入完搜尋條件,直接點擊斷行符號鍵觸發搜尋的js方法

 

1 <li>使用者名稱:<input type="text" class="dfinput" id="queryName" onkeyup="if(event.keyCode==13) queryByName();"  value="${queryName }" style="width: 200px;"></li>2         <li><input  type="button" class="scbtn" onclick="queryByName();" value="查詢"/></li>
View Code

 

sdfa

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.