JavaScript 는 IE 브 라 우 저 이벤트 대상 을 W3C DOM 표준 에 맞 게 합 니 다.

2332 단어 eventW3C

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

좋은 웹페이지 즐겨찾기