这是index.html文件
<!DOCTYPE html>
<html>
<head>
<title>事件</title>
<script src="js/event.js"></script>
<script src="js/script.js"></script>
</head>
<body>
<div id="box">
<input type="button" value="按钮" id="btn1"/>
<a href="event.html" id="go">跳转</a>
</div>
</body>
</html>
这是event.js文件
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;
},
//获取事件类型
getType:function(){
return event.type;
},
//获取事件元素
getElement:function(event){
return event.target||event.srcElement;
},
//阻止冒泡行为
stopPropagation:function(event){
if(event.stopPropagation){
event.stopPropagation();
}
else{
event.cancelBubble=true;
}
},
//阻止默认行为
preventDufault:function(event){
if(event.preventDefault){
event.preventDefault();
}else{
event.returnValue=false;
}
}
}
这是script文件
window.onload=function(){
var btn1=document.getElementById('btn1'),
box=document.getElementById('box'),
go =document.getElementById('go');
eventUtil.addHandler(btn1,'click',function(){
alert('我是一个按钮');
});
eventUtil.addHandler(go,'click',function(event){
alert(eventUtil.getType(event));
});
}
求大神指导!!!