1. 程式人生 > >跨瀏覽器的事件處理程式//IE、chrome

跨瀏覽器的事件處理程式//IE、chrome

var eventUtil={
addHandler:function(element,type,handler){
if(element.addEventListener){
element.addEventListener(type,handler,false);
}else if(element.attachEvent){
element.attachEvent("on"+type,handler);
}else{

element["on"+type]=handler;

}
},
removeHandler:function(element,type,handler){
if(element.removeEventListener){
element.removeEventListener(type,handler,false);
}else if(element.detachEvent){
element.detachEvent("on"+type,handler);
}else{

element["on"+type]=null;

}
},
getEvent:function(event){
return event ? event:window.event;
},
getTarget:function(e){
if(e.target){
return e.target;

}else{
return e.srcElement;
}
},
preventDefault:function(e){
if(e.preventDefault){
e.preventDefault();

}else{
e.returnValue=false;
}
},
stopPropagation:function(e){
if(e.stopPropagation){
e.stopPropagation();

}else{
e.cancelBubble=true;
}
}
};