DIV CSS 佈局教程網

JavaScript 滑鼠與鍵盤事件實例大全
編輯:關於JavaScript     

互動的起點是事件。JavaScript 的滑鼠與鍵盤事件大致分成兩組:鍵盤的 keydown、keyup、keypress,以及滑鼠的 mousedown、mouseup。本文用七個可直接執行的範例,逐一示範常見事件屬性的取得方式。

先釐清觸發時機

按鈕被按下的瞬間觸發 keydown,放開時觸發 keyup。一次完整的 keypress 流程包含「按下」與「鬆開」兩部分,且 keypress 只對能產生字元的按鍵觸發,方向鍵、功能鍵不會觸發它——這是三個事件最關鍵的差異。

滑鼠方面,在元素上按下按鍵觸發 mousedown,在元素上放開則觸發 mouseup。若按下後拖到元素外再放開,mouseup 不會在原元素觸發,這是實作拖曳時必須留意的細節。

1. 判斷被點擊的是左鍵還是右鍵

 <html> <head> <script type="text/javascript"> function whichButton(event) { if (event.button==2) { alert("你點擊了鼠標右鍵!") } else { alert("你點擊了鼠標左鍵!") } } </script> </head> <body onmousedown="whichButton(event)"> <p>請單擊你鼠標的左鍵或右鍵試試</p> </body> </html> 

event.button 為 2 代表右鍵。標準定義是 0 左鍵、1 中鍵、2 右鍵。

2. 取得滑鼠游標的視窗座標

 <html> <head> <script type="text/javascript"> function show_coords(event) { x=event.clientX y=event.clientY alert("X 坐標: " + x + ", Y 坐標: " + y) } </script> </head> <body onmousedown="show_coords(event)"> <p>在此文檔中按下你鼠標的左鍵看看!</p> </body> </html> 

clientX/Y 相對於瀏覽器視窗,捲動頁面時值會隨之改變。

3. 取得被按下按鍵的 Unicode 編碼

 <html> <head> <script type="text/javascript"> function whichButton(event) { alert(event.keyCode) } </script> </head> <body onkeyup="whichButton(event)"> <p>在此文檔中按下你鍵盤上的某個鍵看看</p> </body> </html> 

keyCode 已被標示為不建議使用(deprecated),新程式碼應改用 event.key(回傳實際字元,如 "a"、"Enter")或 event.code(回傳實體按鍵位置,如 "KeyA")。keyCode 的問題在於它對應的是鍵盤按鍵而非字元,不同鍵盤配置的同一位置會得到相同值。

4. 取得滑鼠相對於整個螢幕的座標

 <html> <head> <script type="text/javascript"> function coordinates(event) { x=event.screenX y=event.screenY alert("X=" + x + " Y=" + y) } </script> </head> <body onmousedown="coordinates(event)"> <p> 點擊你鼠標的左鍵 </p> </body> </html> 

screenX/Y 相對於實體螢幕左上角,不受頁面捲動影響。做多螢幕或跨視窗定位時才會用到。

5. 取得游標的頁面座標

 <html> <head> <script type="text/javascript"> function coordinates(event) { x=event.x y=event.y alert("X=" + x + " Y=" + y) } </script> </head> <body onmousedown="coordinates(event)"> <p> 點擊你鼠標的左鍵 </p> </body> </html> 

event.x/event.y 是 IE 引進的寫法,已屬歷史遺留。標準做法請用 pageX/pageY,它給的是相對於整個文件的座標(含捲動量)。

6. 判斷 Shift 鍵是否被按住

 <html> <head> <script type="text/javascript"> function isKeyPressed(event) { if (event.shiftKey==1) { alert("shit鍵按下了!") } else { alert("shit鍵沒有按下!") } } </script> </head> <body onmousedown="isKeyPressed(event)"> <p>按下shit鍵,點擊你鼠標的左鍵</p> </body> </html> 

shiftKey 是布林值,用 === true 比較比與數字 1 比較更合語意。同類屬性還有 ctrlKey、altKey、metaKey(Mac 的 Command 鍵),常用於實作快捷鍵組合。

7. 取得被點擊的元素名稱

 <html> <head> <script type="text/javascript"> function whichElement(e) { var targ if (!e) var e = window.event if (e.target) targ = e.target else if (e.srcElement) targ = e.srcElement if (targ.nodeType == 3) // defeat Safari bug targ = targ.parentNode var tname tname=targ.tagName alert("你點擊了 " + tname + "元素") } </script> </head> <body onmousedown="whichElement(event)"> <p>在這裡點擊看看,這裡是p</p> <h3>或者點擊這裡也可以呀,這裡是h3</h3> <p>你想點我嗎??</p> <img border="0" src="../myCode/btn.gif" width="100" height="26" alt="pic"> </body> </html> 

這段程式示範了三個相容性處理:先用 window.event 兜住舊 IE 的全域 event;再同時檢查 target(標準)與 srcElement(IE);最後判斷 nodeType == 3(文字節點)時改取其父節點,因為 Safari 舊版會把文字節點當成事件目標,而文字節點沒有 tagName。

現代的簡化寫法:

 document.body.addEventListener('mousedown', e => console.log(e.target.tagName));

事件物件自動傳入、target 已標準化、也不必有 nodeType 的顧慮。

事件模型補充

範例中大量使用 onmousedown="..." 這種行內事件寫法,缺點是 HTML 與 JS 職責混在一起、無法一次綁定多個處理器、也不利於 CSP 安全策略。建議改用 addEventListener,並善用事件表徵(event delegation)——把監聽器掛在共同祖先上,透過 e.target 判斷來源,能有效應對動態新增的元素。

若需要完整的事件類型對照表,可參考線上工具 javascript事件與功能說明大全:
https://tools.jb51.net/table/javascript_event

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved