zl程序教程

您现在的位置是:首页 >  前端

当前栏目

JavaScript让IE浏览器event对象符合W3CDOM标准

2023-06-13 09:14:14 时间
复制代码代码如下:

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/>
<title>无标题文档</title>
</head>
<body>
<inputid="butt"type="button"value="提交"/>
</body>
<scripttype="text/javascript">
varEventUtil={};
EventUtil.formatEvent=function(oEvent){
if(window.ActiveXObject){
//W3Cevent的charCode属性,按下的按键的Unicode值
oEvent.charCode=(oEvent.type=="keypress")?oEvent.keyCode:0;
//W3Cevent的eventPhase属性
//事件的阶段,可能有以下的值中的一个:
//0-捕获阶段
//1-在目标上
//2-冒泡阶段
oEvent.eventPhase=2;
//W3Cevent的isChar属性,表示按下的按键是否有字符与之相关
oEvent.isChar=(EventUtil.charCode>0);
//W3Cevent的pageX属性,鼠标相对于页面的X坐标
oEvent.pageX=oEvent.clientX+document.body.scrollLeft;
//W3Cevent的pageY属性,鼠标相对于页面的Y坐标
oEvent.pageY=oEvent.clientY+document.body.scrollTop;
//W3Cevent的preventDefault方法,阻止事件的默认行为
oEvent.preventDefault=function(){
this.returnValue=false;
};
//W3Cevent的relatedTarget属性,事件的第二目标,经常用于鼠标事件
if(oEvent.type=="mouseout"){
oEvent.relatedTarget=oEvent.toElement;
}elseif(oEvent.type=="mouseover"){
oEvent.relatedTarget=oEvent.fromElement;
}
//W3Cevent的stopPropagation方法,取消冒泡事件
oEvent.stopPropagation=function(){
this.cancelBubble=true;
};
//W3Cevent的target属性
oEvent.target=oEvent.srcElement;
//W3Cevent的timestamp属性,创建当前时间,并返回毫秒数
oEvent.time=(newDate()).getTime();
}
returnoEvent;
};
EventUtil.getEvent=function(){
if(window.event){
//IE下返回event对象
returnthis.formatEvent(window.event);
}else{
//W3C下返回event对象
returnEventUtil.getEvent.caller.arguments[0];
}
};
document.getElementById("butt").onclick=function(){
varoEvent=EventUtil.getEvent();
alert(oEvent);
};
</script>
</html>