为div添加事件,不会产生事件冒泡而是弹出两个相同showmes,这是为什么呢?对着视频老师讲解敲了好几回都是这样?拜托哪位大神帮忙解下疑惑,谢谢!
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
</head>
</head>
<body>
<div id="box">
<input type="button" value="按钮3" id="btn3" />
<a href="联系.html" id="go">跳转</a>
</div>
<script type="text/javascript">
var eventUtil={
addHandler:function(element,type,handler){
if(element.addEventListener){
element.addEventListener(type,showmes,false);
}else if(element.attachEvent){
element.attachEvent('on'+type,showmes);
}else{
element['on'+type]=showmes;
}
},
removeHandler:function(element,type,handler){
if(element.removeEventListener){
element.removeEventListener(type,showmes,false);
}else if(element.detachEvent){
element.detachEvent('on'+type,showmes);
}else{
element['on'+type]=null;
}
},
}
function showmes(){
alert('hello world');
}
function showbox(){
alert('这是放按钮的BOX');
}
function stopgoto(){
alert('');
}
var btn3=document.getElementById('btn3');
var box=document.getElementById('box');
var go=document.getElementById('go');
eventUtil.addHandler(btn3,'click',showmes);
eventUtil.addHandler(box,'click',showbox);
</script>
</body>
</html>