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

首頁 > 開發 > JS > 正文

js鼠標按鍵事件和鍵盤按鍵事件用法實例匯總

2024-05-06 16:32:15
字體:
來源:轉載
供稿:網友

本文實例講述了js鼠標按鍵事件和鍵盤按鍵事件用法。分享給大家供大家參考,具體如下:

keydown,keyup,keypress:屬于你的鍵盤按鍵

mousedown,mouseup:屬于你的鼠標按鍵

當按鈕被按下時,發生 keydown 事件,

keyup是在用戶將按鍵抬起的時候才會觸發的,

完整的 key press 過程分為兩個部分:1. 按鍵被按下;2. 按鍵被松開。

當用戶在這個元素上按下鼠標鍵的時候,發生mousedown

當用戶在這個元素上松開鼠標鍵的時候,發生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>

2. 當前鼠標的光標坐標是多少

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

3. 被按下鍵的unicode碼是多少

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

4. 當前鼠標的光標相對于屏幕的坐標是多少

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

5. 當前鼠標的光標坐標是多少

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

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>

7. 當前被點擊的是哪一個元素

<html><head><script type="text/javascript">function whichElement(e){var targif (!e) var e = window.eventif (e.target) targ = e.targetelse if (e.srcElement) targ = e.srcElementif (targ.nodeType == 3) // defeat Safari bugtarg = targ.parentNodevar tnametname=targ.tagNamealert("你點擊了 " + 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>

希望本文所述對大家JavaScript程序設計有所幫助。


注:相關教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 马龙县| 娱乐| 湟中县| 应用必备| 志丹县| 山阳县| 惠来县| 高台县| 阜新市| 罗源县| 平顶山市| 仪征市| 曲靖市| 兰州市| 钟山县| 北碚区| 邵阳县| 石渠县| 洪湖市| 大安市| 星座| 祁门县| 启东市| 拉萨市| 白玉县| 美姑县| 南部县| 大埔区| 深州市| 宝清县| 武城县| 濉溪县| 广饶县| 泰安市| 民权县| 北安市| 思南县| 兴海县| 江安县| 永顺县| 晋城|