日期:2014-05-16  浏览次数:20282 次

JS event对象 简单描述

Event代表事件状态,如事件发生的元素,键盘状态,鼠标位置和鼠标按钮状态。一旦事件发生,便会生成Event对象,如单击一个按钮,浏览器的内存中就产生相应的 event对象。
event 对象只在事件发生的过程中才有效。
event的某些属性只对特定的事件有意义。比如,fromElement 和 toElement 属性只对 onmouseover 和 onmouseout 事件有意义。

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

--------------------------------------------

1.altKey
描述:检查alt键的状态。

语法:event.altKey

可能的值:
当alt键按下时,值为 TRUE ,否则为 FALSE 。只读。


2.button
描述:检查按下的鼠标键。

语法:event.button

可能的值:
0 没按键
1 按左键
2 按右键
3 按左右键
4 按中间键
5 按左键和中间键
6 按右键和中间键
7 按所有的键

这个属性仅用于onmousedown, onmouseup, 和 onmousemove 事件。对其他事件,不管鼠标状态如何,都返回 0(比如onclick)。



3.cancelBubble
描述:检测是否接受上层元素的事件的控制。

语法:
event.cancelBubble[ = cancelBubble]

可能的值:
这是一个可读写的布尔值:

TRUE 不被上层原素的事件控制。
FALSE 允许被上层元素的事件控制。这是默认值。

例子:
下面的代码片断演示了当在图片上点击(onclick)时,如果同时shift键也被按下,就取消上层元素(body)上的事件 onclick所引发的showSrc()函数。

<SCRIPT LANGUAGE="JScript">
function checkCancel() {
    if (window.event.shiftKey)
        window.event.cancelBubble = true;
}
function showSrc() {
    if (window.event.srcElement.tagName == "IMG")
        alert(window.event.srcElement.src);
}
</SCRIPT>
<BODY onclick="showSrc()">
<IMG onclick="checkCancel()" SRC="sample.gif">


4.clientX
描述:返回鼠标在窗口客户区域中的X坐标。

语法:event.clientX

注释:
这是个只读属性。这意味着,你只能通过它来得到鼠标的当前位置,却不能用它来更改鼠标的位置。


5.clientY
描述:返回鼠标在窗口客户区域中的Y坐标。

语法:event.clientY

注释:
这是个只读属性。这意味着,你只能通过它来得到鼠标的当前位置,却不能用它来更改鼠标的位置。


6.ctrlKey
描述:检查ctrl键的状态。

语法:event.ctrlKey

可能的值:
当ctrl键按下时,值为 TRUE ,否则为 FALSE 。只读。


7.fromElement
描述:检测 onmouseover 和 onmouseout 事件发生时,鼠标所离开的元素。 参考:18.toElement

语法:event.fromElement

注释:
这是个只读属性。


8.keyCode
描述:检测键盘事件相对应的内码。这个属性用于 onkeydown, onkeyup, 和 onkeypress 事件。

语法:event.keyCode[ = keyCode]

可能的值:
这是个可读写的值,可以是任何一个Unicode键盘内码。如果没有引发键盘事件,则该值为 0 。


9.offsetX
描述:检查相对于触发事件的对象,鼠标位置的水平坐标

语法:event.offsetX


10.offsetY
描述:检查相对于触发事件的对象,鼠标位置的垂直坐标

语法:event.offsetY


11.propertyName
描述:设置或返回元素的变化了的属性的名称。

语法:event.propertyName [ = sProperty ]

可能的值:
sProperty 是一个字符串,指定或返回触发事件的元素在事件中变化了的属性的名称。
这个属性是可读写的。无默认值。

注释:
你可以通过使用 onpropertychange 事件,得到 propertyName 的值。

例子:
下面的例子通过使用 onpropertychange 事件,弹出一个对话框,显示 propertyName 的值。

<HEAD>
<SCRIPT>
function changeProp()
{
    btnProp.value = "This is the new VALUE";
}

function changeCSSProp()
{
    btnStyleProp.style.backgroundColor = "aqua";
}
</SCRIPT>
</HEAD>
<BODY>
<P>The event object property propertyName is
used here to return which property has been
altered.</P>

<INPUT TYPE=button ID=btnProp onclick="changeProp()"
VALUE="Click to change the VALUE property of this button"
onpropertychange='alert(event.propertyName+" property has changed value")'>
<INPUT TYPE=button ID=btnStyleProp
onclick="changeCSSProp()"
VALUE="Click to change the CSS backgroundColor property of this button"
onpropertychange='alert(event.propertyName+" property has changed value")'>