var eventUtil={
//添加句柄
//element,节点
//type,事件类型
//handler,函数
addHandler:function(element,type,handler){
if(element.addEventListener){
element.addEventListener(type,handler,false);
}else if(elemnt.attachEvent){//对ie进行判断
element.attachEvent(‘on‘+type,handler)
}else{//
element[‘on‘+type]+handler;
}
},
removeHandler:function(element,type,handler){
element.removeEventListener(type,handler,false);
}else if(element.detachEvent){//detachEvent是ie的事件监听
element.detachEvent(‘on‘+type,handler){
}else{
element[‘on‘+type]=null;
}
}
getEvent:function(event){ //获取事件对象
return event?event:window.event;
},
getType:function(event){ //获取事件类型
return event.type;
},
getElement:function(event){ //获取事件的节点
return event.target||event.srcElement;
},
preventDefualt:function(event){ //阻止时间冒泡行为
if(event.preventDefault){
event.preventDefault;
}else{
event.returnValue=false;
}
},
stopPropagation:function(event){ //阻止默认行为
if(event.stopPrapagation){
event.stopPropagation();
}esle{
event.cancelbubble=true;
}
}
}
调用的js
eventUtil.addhandler(element,type ,handler);
eventUtil.removehandler(element,type ,handler);