<div id="id1" style="width:200px; height:200px; position:absolute; top:100px; left:100px; background-color:blue; z-index:4">
<div id="id2" style="width:200px; height:200px; position:absolute; top:20px; left:70px; background-color:green; z-index:1"></div>
</div>
addEventListener中的第三个参 数是useCapture, 一个bool类型。当为false时为冒泡获取(由里向外),true为capture方式(由外向里)。
eg1:
document.getElementById('id1').addEventListener('click', function() { console.log('id1');}, false);
document.getElementById('id2').addEventListener('click', function() { console.log('id2');}, false);
点击id2的div结果是: id2, id1
document.getElementById('id1').addEventListener('click', function() { console.log('id1');}, false);
document.getElementById('id2').addEventListener('click', function() { console.log('id2');}, true);
结果是: id2, id1
document.getElementById('id1').addEventListener('click', function() { console.log('id1');}, true);
document.getElementById('id2').addEventListener('click', function() { console.log('id2');}, false);
结果是:id1,id2
document.getElementById('id1').addEventListener('click', function() { console.log('id1');}, true);
document.getElementById('id2').addEventListener('click', function() { console.log('id2');}, true);
结果是:id1,id2
---------------------
【转载,仅作分享,侵删】
作者:丿灬安之若死
原文:https://blog.csdn.net/mp624183768/article/details/87887128
版权声明:本文为博主原创文章,转载请附上博文链接!
|
|