Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

addEventListener的第三个参数,在哪些事件中或者说在哪些具体场合要用true呢


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
260 views
Welcome To Ask or Share your Answers For Others

1 Answer

<div id="s1">
  <div id="s2"></div>
</div>

第三个参数 false 和 true 分别对应 事件冒泡 和 事件捕获
假设上面的代码, s1和s2都用addEventListener绑定了点击事件
当false时, 点击s2, 则先执行s2, 再执行s1, 即从点击的元素开始往父级冒泡
当true是, 点击s2, 则先执行s1, 再执行s2, 即按document -> html -> body -> s1 -> s2的顺序捕获


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...