点击事件阻止缺省事件以及阻止冒泡

 1 function stopEvent(e){
 2     stopDefault(e);
 3     stopBubble(e);
 4 }
 5 
//阻止缺省事件发生 6 function stopDefault(e){ 7 if(e.preventDefault){ 8 e.preventDefault(); 9 }else{ 10 e.returnValue=false; 11 } 12 } 13 //阻止冒泡发生 14 function stopBubble(e){ 15 if(e.stopPropagation){ 16 e.stopPropagation(); 17 }else{ 18 e.cancelBubble=true; 19 } 20 }

使用:
<div onclick="alert('div')" >
<a href="http://www.baidu.com" onclick="stopEvent(event)" >点我</a>
</div>

 

posted @ 2014-10-15 10:04  悄悄靠近你  阅读(247)  评论(0编辑  收藏  举报