事件冒泡和捕获的问题。

请问大神们,我在最外层div加个点击事件,里面放个a标签,点击a标签也可以产生冒泡到外层div?是因为它属于外层div的原因么?

难道不是需要两个都绑定事件,里层才能冒泡到最外面来么?

那么反之事件捕获呢? 小白不太理解。。。


阿晨1998
浏览 429回答 1
1回答

一只名叫tom的猫

请问大神们,我在最外层div加个点击事件,里面放个a标签,点击a标签也可以产生冒泡到外层div?是的。是因为它属于外层div的原因么?因为在结构上,它们是有层级关系的(父子关系)。难道不是需要两个都绑定事件,里层才能冒泡到最外面来么?不是。那么反之事件捕获呢? 小白不太理解。。。一样的。理解的时候,注意想明白,节点范围的“大小”就行了,事件触发一定是不能跳过“大”节点的嘛。<!DOCTYPE html><html><head><meta charset="utf-8" /><title>事件</title><link rel="stylesheet" type="text/css" href="" /></head><body>&nbsp; <div id="out" style="background-color: yellow;">&nbsp; &nbsp; <div id="middle" style="background-color: red; width: 50%;">&nbsp; &nbsp; &nbsp; <a href="#" id="inner" style="background-color: green;">里面的东西</a>&nbsp; &nbsp; </div>&nbsp; </div>&nbsp; <script type="text/javascript">&nbsp; &nbsp; // 下传, 捕捉&nbsp; &nbsp; document.getElementById('out').addEventListener('click', function(e){&nbsp; &nbsp; &nbsp; console.log('1');&nbsp; &nbsp; }, true);&nbsp; &nbsp; // 上浮, 冒泡&nbsp; &nbsp; document.getElementById('out').addEventListener('click', function(e){&nbsp; &nbsp; &nbsp; console.log('2');&nbsp; &nbsp; }, false);&nbsp; </script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript