您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

关于js鼠标按键事件和键盘按键事件的使用方法

2024/3/15 22:43:26发布23次查看
这篇文章主要介绍了js鼠标按键事件和键盘按键事件用法,结合实例形式总结分析了javascript针对鼠标与键盘事件的常用操作技巧,需要的朋友可以参考下
本文实例讲述了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>
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注!
相关推荐:
关于js的函数调用栈stack size的计算方法
javascript图片放大技术的实现
以上就是关于js鼠标按键事件和键盘按键事件的使用方法的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product