为什么不能阻止冒泡和默认行为
// JavaScript Document //这是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.attachEvent) { 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.nodeName||event.srcElement; }, //阻止事件的默认行为 preventDefault:function(event) { if(event.preventDefault)//判断时方法不加括号 { event.preventDefault(); } else { event.returnValue=false; } }, //阻止冒泡 stopPropagation:function(event) { if(event.stopPropagation) { event.stopPropagation(); } else { event.cancelBubble=true; } }, }
//这是script.js // JavaScript Document window.onload=function() { var go=document.getElementById('go'); var box=document.getElementById('box'); eventUtil.addHandler(box,'click',function() { alert('我是盒子box!'); }); eventUtil.addHander(go,'click',function(e) {e=eventUtil.getEvent(e); alert(eventUtil.getElement(e)); eventUtil.preventDefault(e); eventUtil.stopPropagation(e); }); } 这是HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>事件处理程序</title> <script src="event.js"></script> <script src="script.js"></script> </head> <body> <div id="box"> <input type="button" id="btn" value="按钮" onclick="showMes()"/> <input type="button" id="btn2" value="按钮2" /> <input type="button" id="btn3" value="按钮3" /> <a href="HelloWorld.html" id="go">跳转</a> </div> </body> </html>
为什么不能阻止冒泡和默认行为??求解