JavaScript让IE浏览器event对象符合W3C DOM标准
代码如下:
<!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> </head> <body> <input id="butt" type="button" value="提交" /> </body> <script type="text/javascript"> var EventUtil = {}; EventUtil.formatEvent = function(oEvent){ if(window.ActiveXObject){ //W3C event的charCode属性,按下的按键的Unicode值 oEvent.charCode = (oEvent.type == 'keypress') ? oEvent.keyCode : 0; //W3C event的eventPhase属性 //事件的阶段,可能有以下的值中的一个: // 0 - 捕获阶段 // 1 - 在目标上 // 2 - 冒泡阶段 oEvent.eventPhase = 2; //W3C event的isChar属性,表示按下的按键是否有字符与之相关 oEvent.isChar = (EventUtil.charCode > 0); //W3C event的pageX属性,鼠标相对于页面的X坐标 oEvent.pageX = oEvent.clientX + document.body.scrollLeft; //W3C event的pageY属性,鼠标相对于页面的Y坐标 oEvent.pageY = oEvent.clientY + document.body.scrollTop; //W3C event的preventDefault方法,阻止事件的默认行为 oEvent.preventDefault = function(){ this.returnValue = false; }; //W3C event的relatedTarget属性,事件的第二目标,经常用于鼠标事件 if(oEvent.type == 'mouseout'){ oEvent.relatedTarget = oEvent.toElement; }else if(oEvent.type == 'mouseover'){ oEvent.relatedTarget = oEvent.fromElement; } //W3C event的stopPropagation方法,取消冒泡事件 oEvent.stopPropagation = function(){ this.cancelBubble = true; }; //W3C event的target属性 oEvent.target = oEvent.srcElement; //W3C event的timestamp属性,创建当前时间,并返回毫秒数 oEvent.time = (new Date()).getTime(); } return oEvent; }; EventUtil.getEvent = function(){ if(window.event){ // IE 下返回event对象 return this.formatEvent(window.event); }else{ // W3C 下返回event对象 return EventUtil.getEvent.caller.arguments[0]; } }; document.getElementById('butt').onclick = function(){ var oEvent = EventUtil.getEvent(); alert(oEvent); }; </script> </html>
相关推荐
书虫媛 2020-02-02
liaoxuewu 2020-01-18
honeyth 2020-09-13
mjshldcsd 2020-08-15
Ping 2020-08-15
Kakoola 2020-08-01
Yellowpython 2020-08-01
chenguangchun 2020-07-26
dailinqing 2020-07-18
xiaouncle 2020-07-05
delmarks 2020-06-28
MayerF 2020-06-14
MrHaoNan 2020-06-13
smalllove 2020-06-09
JayFighting 2020-06-08
xiaoge00 2020-06-07
haokele 2020-05-31
matthewhan 2020-05-25