10.冒泡和捕获(bubble和capture)以及目标target
马 克-to-win:当几个元素重合(必须元素嵌套,像12.2例子那样,元素不嵌套,则同样事件肯定先进入上层元素,因为理论上两个元素没重合,根本就是 两个独立的元素,这时肯定事件先进入上层元素),事件先捕获(从底层向上层),后冒泡(从上层向底层)。最顶层的是target。
例 10.1(bubbleIEFF.html)
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script>
window.onload = function(){undefined
var wai = document.getElementById("wai");
var zhong = document.getElementById("zhong");
var nei = document.getElementById("nei");
// 目标(是冒泡还是捕获无所谓)
nei.addEventListener('click',function(){alert("目标");},true);
// 事件冒泡
wai.addEventListener('click',function(){alert("wai冒泡");},false);
zhong.addEventListener('click',function(){alert("中冒泡");},false);
// 事件捕获
wai.addEventListener('click',function(){alert("wai捕获");},true);
zhong.addEventListener('click',function(){alert("中捕获");},true);
};
</script>
<body>
<div id="wai" style="width:300px; height:300px; background:#F00;position:relative;">
<div id="zhong" style="width:300px;height:300; background:#0f0;position:relative;">
<div id="nei" style="width:300px;height:300px; background:#00f;position:relative;"></div>
</div>
</div>
</body>
更多内容请见原文,文章转载自:https://blog.csdn.net/qq_43650923/article/details/102824023