js 阻止冒泡和默认行为
js 阻止冒泡和默认行为
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>标题</title>
<style type="text/css">
*{margin:0;padding:0;}
.wrapper{display:block;width:100px;height:100px;margin:0 auto;border:1px solid red;}
</style>
</head>
<body>
<a href="#" target="_blank" class="wrapper" id="wrapper"></a>
<script type="text/javascript">
window.onload = function(){
var oWrapper = getId("wrapper");
oWrapper.onclick = function(e){
var oEvent = e || event;
if(oEvent.stopPropagation){ //firefox
oEvent.stopPropagation(); // 阻止冒泡
oEvent.preventDefault(); // 阻止默认时间
}else{
// IE
oEvent.cancelBubble = true; // 阻止冒泡
oEvent.returnValue = false; // 阻止默认时间
}
alert("a");
}
document.onclick = function(){
alert("b");
}
}
function getId(id){
return document.getElementById(id)
}
</script>
</body>
</html>