點擊事件阻止缺省事件以及阻止冒泡
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 onclick="stopEvent(event)" >點我</a>
</div>