window.event(转自:http://blog.csdn.net/cnkiminzhuhu/archive/2008/02/22/2112408.aspx)

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

例子一、

下面的例子检查鼠标是否在链接上单击,并且,如果shift键被按下,就取消链接的跳转。

 <HTML>
<HEAD><TITLE>Cancels Links</TITLE>
<SCRIPT LANGUAGE="JScript">
function cancelLink() {
if (window.event.srcElement.tagName == "A" && window.event.shiftKey)
window.event.returnValue = false;
}
</SCRIPT>
<BODY οnclick="cancelLink()">
属性:

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">
12.returnValue
描述:
设置或检查从事件中返回的值

语法:
event.returnValue[ = Boolean]

可能的值:
true 事件中的值被返回
false 源对象上事件的默认操作被取消
详情见例子一、

16.srcElement
描述:
返回触发事件的元素。只读。例子见本文开头。

语法:
event.srcElement
例:

   
   
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>

< html  xmlns ="http://www.w3.org/1999/xhtml"  xml:lang ="en"  lang ="en" >
< head >
  
< title ></ title >
  
< script  type ="text/javascript" >
    
function  tdclick() {
        
if (event.srcElement.tagName.toLowerCase() == ' td ' )
            alert(
" 行: " + (event.srcElement.parentNode.rowIndex + 1 ) + " 列: " + (event.srcElement.cellIndex + 1 ));
    }

  
</ script >
</ head >
< body >
< table  align ="center"  onclick ="tdclick()"  width ="100%"  height ="400"  cellspacing ="1"  border ="1"  bordercolor ="#000000"  bordercolorlight ="#000000"  bordercolordark ="#C0C0C0"  bgcolor ="#C0C0C0" >
  
< tr >
    
< td > &nbsp; </ td >
    
< td > &nbsp; </ td >
    
< td > &nbsp; </ td >
    
< td > &nbsp; </ td >
  
</ tr >
  
< tr >
    
< td > &nbsp; </ td >
    
< td > &nbsp; </ td >
    
< td > &nbsp; </ td >
    
< td > &nbsp; </ td >
  
</ tr >
  
< tr >
    
< td > &nbsp; </ td >
    
< td > &nbsp; </ td >
    
< td > &nbsp; </ td >
    
< td > &nbsp; </ td >
  
</ tr >
</ table >
</ body >
</ html >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值