当前位置: 首页 > 图文教程 > 网络编程 > Javascript > JavaScript让IE浏览器event对象符合W3C DOM标准

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 JavaScript让IE浏览器event对象符合W3C DOM标准


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-10   浏览: 437 ::
收藏到网摘: n/a

IE浏览器event对象跟W3C实现的不一样.所以自己封装一个EventUtil类来让IE浏览器的event对象与W3C一样.
复制代码 代码如下:

<!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>