<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<style>
.parent{ border: #ccc solid 1px; width: 100px; height: 100px; padding: 100px; }
</style>
<div class="parent">
<button class="btn" id="btn">点击</button>
</div>
<script>
var btn = document.querySelector('.btn');
btn.onclick = function(e){
alert('btn');
// 兼容ie和标准浏览器
var e = e || window.event;
// 判断是否是点击自己
if ( e.target === this || e.srcElement === this) {
if (e.stopPropagation) {
e.stopPropagation(); // 标准浏览器用的
} else {
e.cancleBubble = false; // ie6-8 浏览器取消事件冒泡
}
}
};
var parent = document.querySelector('.parent');
parent.onclick = function(){
alert('parent');
};
</script>
</body>
</html>