`
q272156430
  • 浏览: 269907 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

window.event对象详细介绍

阅读更多

1、event代表事件的状态,例如触发event对象的元素、鼠标的位置及状态、按下的键等等。event对象只在事件发生的过程中才有效。event的某些属性只对特定的事件有意义。比如,fromElement 和 toElement 属性只对 onmouseover 和 onmouseout 事件有意义。

2、属性:
altKey, button, cancelBubble, clientX, clientY, ctrlKey, fromElement, keyCode, offsetX, offsetY, propertyName, returnValue, screenX, screenY, shiftKey, srcElement, srcFilter, toElement, type, x, y


3、属性详细说明:


属性名 描述 值 说明
altKey 检查alt键的状态 当alt键按下时,值为True否则为False 只读
shiftKey 检查shift键的状态 当shift键按下时,值为True否则为False 只读
ctrlKey 检查ctrl键的状态 当ctrl键按下时,值为True否则为False 只读
例:(点击按钮时显示那几个特殊键按下的状态)
<input type="button" value="点击" onClick="showState()"/>
<script>
function show(){
 alert("altKey:"+window.event.altKey
  +"\nshiftKey:"+window.event.shiftKey
  +"\nctrlKey:"+window.event.ctrlKey);
}</script>
 keyCode  检测键盘事件相对应的内码    可读写,可以是任何一个Unicode键盘内码。如果没有引发键盘事件,则该值为0
例:(按回车键让下一组件得到焦点,相当按Tab键)
<input type="text" onKeyDown="nextBlur()"/>
<input type="text"/>
<script>
function nextBlur(){
 if(window.event.keyCode==13)//回车键的 code
  window.event.keyCode=9;//Tab键的code
}
</script>
 srcElement  返回触发事件的元素  Object  只读
例:(点击按钮时显示按钮的name值)
<input type="button" value="闽" name="福建" onClick="show()"/>
<input type="button" value="赣" name="江西" onClick="show()"/>
<script>
function show(){
 alert(window.event.srcElement.name);
}
</script>
 x,y  鼠标相对于当前浏览器的位置  px  只读
 clientX,clientY  鼠标当前相对于网页的位置  px  只读
 offsetX,offsetY  鼠标当前相对于网页中的某一区域的位置  px  只读
 screenX,screenY  相对于用户显示器的位置  px  只读
说明:当你点击一个按钮时得到(x,clientX,offsetX,screenX)很容易明白offsetX;当你把IE窗口还原后得到(x,clientX,screenX),你就会明白screenX;当你把div的属性position在absolute和relative之间切换时,你就会明白x和clientX的区别。
 returnValue  设置或检查从事件中返回的值  true 事件中的值被返回
false 源对象上事件的默认操作被取消  可读写
例如:屏蔽鼠标右键、Ctrl+n、shift+F10、F5刷新、退格键
function KeyDown(){
 //屏蔽鼠标右键、Ctrl+N、Shift+F10、F5刷新、退格键
  if ((window.event.altKey)&&
      ((window.event.keyCode==37)||   //屏蔽 Alt+ 方向键 ←
       (window.event.keyCode==39))){  //屏蔽 Alt+ 方向键 →
     event.returnValue=false;//防止使用ALT+方向键前进或后退网页
  }
  if ((event.keyCode==8) ||      //屏蔽退格删除键
      (event.keyCode==116)||   //屏蔽 F5 刷新键
      (event.keyCode==112)||   //屏蔽 F1 刷新键 bitsCN.com中国网管联盟
      (event.ctrlKey && event.keyCode==82)){ //Ctrl + R
     event.keyCode=0;
     event.returnValue=false;
  }
  if ((event.ctrlKey)&&(event.keyCode==78))   //屏蔽Ctrl+N
     event.returnValue=false;
  if ((event.shiftKey)&&(event.keyCode==121)) //屏蔽Shift+F10
     event.returnValue=false;
  if (window.event.srcElement.tagName == "A" && window.event.shiftKey)
      window.event.returnValue = false;  //屏蔽 shift 加鼠标左键新开一网页
  if ((window.event.altKey)&&(window.event.keyCode==115)){ //屏蔽Alt+F4
      window.showModelessDialog("about:blank","","dialogWidth:1px;dialogHeight:1px");
      return false;}
}
 button  检查按下的鼠标键  0 没按键
1 按左键
2 按右键
3 按左右键
4 按中间键
5 按左键和中间键
6 按右键和中间键
7 按所有的键  仅用于onmousedown,onmouseup和onmousemove事件。对其他事件,不管鼠标状态如何,都返回0(比如onclick)
 srcElement  检测onmouseover和onmouseout事件发生时,鼠标所离开的元素  Object  只读
 toElement  检测onmouseover和onmouseout事件发生时,鼠标所进入的元素  Object  只读
 type  返回事件名    返回没有“on”作为前缀的事件名,比如,onclick事件返回的type是click

 

分享到:
评论

相关推荐

    window.event 对象详解

    window.event 对象详解

    [JS]详尽解析window.event对象

    event代表事件的状态,例如触发event对象的元素、鼠标的位置及状态、按下的键等等。 event对象只在事件发生的过程中才有效。 event的某些属性只对特定的事件有意义。比如,fromElement 和 toElement 属性只对 ...

    高手window.event对象详解

    window.event对象详解。event对象只在事件发生的过程中才有效;event的某些属性只对特定的事件有意义。比如,fromElement 和 toElement 属性只对 onmouseover 和 onmouseout 事件有意义。

    window.event.srcElement 得到事件源对象

    event对象指当前触发的事件对象, window.event.srcElement是指触发事件的对象。比如你设定[removed] = myfunc;这时所有页面点击的事件都交给myfunc处理,在myfunc函数里可以写vSrc = window.event.srcElement,知道

    js window.event对象详尽解析

    event代表事件的状态,例如触发event对象的元素、鼠标的位置及状态、按下的键等等。 event对象只在事件发生的过程中才有效。 event的某些属性只对特定的事件有意义。比如,fromElement 和 toElement 属性只对 ...

    window.event.keyCode兼容IE和Firefox实现js代码

    input type=”text” onkeydown=”keyNumAll... evt : ((window.event) ? window.event : “”); var key = evt.keyCode?evt.keyCode:evt.which;//兼容IE和Firefox获得keyBoardEvent对象的键值 console.info(key);/

    Event对象详解

    event代表事件的状态,例如触发event对象的元素、鼠标的位置及状态、按下的键等等。 event对象只在事件发生的过程中才有效。 event的某些属性只对特定的事件有意义。比如,fromElement 和 toElement 属性只对 ...

    js鼠标点击事件在各个浏览器中的写法及Event对象属性介绍

    IE 左键是 window.event.button = 1 右键是 window.event.button = 2 中键是 window.event.button = 4 没有按键动作window.event.button = 0 Firefox 左键是 event.button = 0 右键是 event.button = 2 中键是 event...

    js兼容问题 详解

    IE:有window.event对象 FF:没有window.event对象。可以通过给函数的参数传递event对象。如onmousemove=doMouseMove(event) 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。...

    window对象--event对象详解

    NULL 博文链接:https://bei-jin-520.iteye.com/blog/313444

    javascript中window.event事件用法详解

    前两天写程序时因为要用到javascript中的window.event事件,于是就在网上搜了一下,终于找到一篇不错的文章,来与大家分享下: 描述 event代表事件的状态,例如触发event对象的元素、鼠标的位置及状态、按下的键等等...

    整理的比较全的event对像在ie与firefox浏览器中的区别

    window.event IE:有window.event对象 FF:没有window.event对象。可以通过给函数的参数传递event对象。如onmousemove=doMouseMove(event) 鼠标当前坐标 IE:event.x和event.y。 FF:event.pageX和event.pageY。 ...

    Javascript下IE与Firefox下的差异兼容写法总结

    window.event对象差异 IE:有window.event对象 FF:没有window.event对象。可以通过给函数的参数传递event对象。如onmousemove=doMouseMove(event) 获取鼠标当前坐标 IE:event.x和event.y。 FF:event.pageX和event...

    js事件源window.event.srcElement兼容性写法(详解)

    如下所示: &lt;... &lt;body&gt; 一个好处就是 我想让body(或其他元素内)的某些对象响应事件 就不用挨个儿去写 ...&lt;p&gt;event对象指当前触发的事件对象, window.event.srcElement是指触发事件的对象。比如你

    比较全面的event对像在IE与FF中的区别 推荐

    window.event IE:有window.event对象 FF:没有window.event对象。可以通过给函数的参数传递event对象。如onmousemove=doMouseMove(event) 鼠标当前坐标 IE:event.x和event.y。 FF:event.pageX和event.pageY。 ...

    IE和FF在对js支持的不同(整理)及解决方法

    1.window.event IE:有window.event对象 FF:没有window.event对象,可以通过函数的参数传递event对象。如onclick=clickHandler(event) 解决办法:var event = event || window.event; 2. 鼠标当前坐标 IE:event.x...

    ie与firefox下的event使用说明与详细区别

    [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]下面是关于ie与firefox event的详细区别window.event IE:有window.event对象 FF:没有window.event对象。可以通过给函数的参数传递event对象。如onmousemove=...

Global site tag (gtag.js) - Google Analytics