Because the events of IE are global and the events of Firefox are local, it is not convenient to use. At this time, we have to assemble the commonly used event operations ourselves and encapsulate them into classes for easy reuse
/**
Class Event
Usage:
Event.getEvent(); Get the event of ie, firefox
Event.getTarget(); Get the srcElement of ie or the target of firefox
Event.isIe() ;Whether it is ie
Event.clientX(); Get the mouse x coordinate of ie,fox
Event.clientY(); Get the mouse y coordinate of ie,fox
*/
var Event=new function(){
this.toString=function(){
return this.getEvent();
}
//Get the event
this.getEvent=function(){
var ev=window.event;
if(!ev){
var c=this.getEvent.caller;
while(c){
ev=c.arguments[0];
if(ev && Event = =ev.constructor)
break;
c=c.caller;
}
}
return ev;
};
//Get event source
this .getTarget=function(){
var ev=this.getEvent();
return this.isIe()?ev.srcElement:ev.target;
}
//Is it ie
this.isIe=function(){
return document.all?true:false;
}
//Mouse x coordinate
this.clientX=function(){
var ev =this.getEvent();
var x=this.isIe()?ev.clientX:ev.pageX;
return x;
}
//Mouse y coordinate
this. clientY=function(){
var ev=this.getEvent();
var y=this.isIe()?ev.clientY:ev.pageY;
return y;
}
/**Add event (object, event type, function pointer)
obj: html object
sEvent: event name
spNotify: event execution method
isCapture: whether to allow full screen capture
*/
this.addEvent=function(obj,sEvent,fpNotify,isCapture){
sEvent=sEvent.indexOf("on")!=-1?sEvent:"on" sEvent;
if(obj.addEventListener){
sEvent=sEvent.substring(sEvent.indexOf("on") 2);
obj.addEventListener(sEvent,fpNotify,isCapture);
} else{ //ie
if(isCapture)
obj.setCapture(isCapture);
obj.attachEvent(sEvent,fpNotify);
}
}
//Remove event
this.removeEvent=function(obj,sEvent,fpNotify){
if(obj.removeEventListener){
sEvent=sEvent.substring(sEvent.indexOf("on") 2)
obj. removeEventListener(sEvent,fpNotify,false);
}else{
obj.detachEvent(sEvent,fpNotify);
}
}
//Get the mouse button, left=1, middle= 2,right=3
this.button=function(){
var ev=this.getEvent();
if(!ev.which&&ev.button){//ie
return ev. button&1?1:(ev.button&2?3:(ev.button&4?2:0))
}
return ev.which;
};
//Prevent event bubbling delivery
this.stopPropagation=function(){
var ev=this.getEvent();
if(this.isIe)
ev.cancelBubble=true;
else
ev.stopPropagation( );
}
//Prevent the default event from returning
this.preventDefault=function(){
var ev=this.getEvent();
if(this.isIe)
ev.returnValue=false;
else
ev.preventDefault();
}
}