下面的代码会执行三次alert:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div οnclick="alert('我是第一层')">
<div οnclick="alert('我是第二层')">
<input type="checkbox" οnclick="alert('checkbox的点击事件');"/>
</div>
</div>
</body>
</html>
加上停止冒泡的处理代码即可:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div οnclick="alert('我是第一层')">
<div οnclick="alert('我是第二层')">
<input type="checkbox" οnclick="alert('checkbox的点击事件');event.stopPropagation();"/>
</div>
</div>
</body>
</html>