事件冒泡和捕获之间的区别是什么?什么时候应该使用冒泡和捕获?
当前回答
DOM Events描述了事件传播的3个阶段:捕获阶段——事件向下到元素。目标阶段——事件到达目标元素。冒泡阶段——事件从元素冒泡。
其他回答
我发现这个教程在javascript.info中非常清楚地解释了这个主题。它最后的3点总结是对关键点的讨论。我在这里引用:
事件首先被捕捉到最深的目标,然后向上冒泡。在 IE<9,它们只是冒泡。 所有处理程序都在冒泡阶段上工作,除了 addEventListener的最后一个参数为真,这是唯一的方法 在捕获阶段捕获事件。 冒泡/捕获可以 由事件停止。cancelBubble=true (IE)或event.stopPropagation() 对于其他浏览器。
描述:
quirksmode。org对此有一个很好的描述。简而言之(摘自quirksmode):
Event capturing When you use event capturing | | ---------------| |----------------- | element1 | | | | -----------| |----------- | | |element2 \ / | | | ------------------------- | | Event CAPTURING | ----------------------------------- the event handler of element1 fires first, the event handler of element2 fires last. Event bubbling When you use event bubbling / \ ---------------| |----------------- | element1 | | | | -----------| |----------- | | |element2 | | | | | ------------------------- | | Event BUBBLING | ----------------------------------- the event handler of element2 fires first, the event handler of element1 fires last.
用什么?
这取决于你想做什么。没有更好的了。不同之处在于事件处理程序的执行顺序。大多数情况下,在冒泡阶段触发事件处理程序是可以的,但也有必要在更早的时候触发它们。
正如其他人所说,冒泡和捕获描述了一些嵌套元素接收给定事件的顺序。
我想指出,对于最里面的元素可能会出现一些奇怪的东西。实际上,在这种情况下,添加事件侦听器的顺序确实很重要。
在下面的例子中,捕获div2将先执行,然后再执行冒泡;当为div4冒泡时,将先执行而不是捕获。
function addClickListener (msg, num, type) { document.querySelector("#div" + num) .addEventListener("click", () => alert(msg + num), type); } bubble = (num) => addClickListener("bubble ", num, false); capture = (num) => addClickListener("capture ", num, true); // first capture then bubble capture(1); capture(2); bubble(2); bubble(1); // try reverse order bubble(3); bubble(4); capture(4); capture(3); #div1, #div2, #div3, #div4 { border: solid 1px; padding: 3px; margin: 3px; } <div id="div1"> div 1 <div id="div2"> div 2 </div> </div> <div id="div3"> div 3 <div id="div4"> div 4 </div> </div>
编辑:这种行为可能因浏览器而异(例如,目前在Firefox上出现,但在Chrome和Edge上没有)。然而,我认为人们应该意识到这一点。
当浏览器检测到事件时,它会尝试查找事件处理程序。这个过程有三个阶段。假设我们有这些元素
<body>
<div>
<button>click</button>
</div>
</body>
1-Capture阶段
browser is going to take a look at the element that was just clicked. Then it will go to the top parent element which is body. if there is any click handle in body, the browser will call it. after checking the body element, it will look at the second top parent element which is div element. This process will repeat until the browser gets down to the button element at the very bottom. Once it sees the button element, Phase One- Capturing will be over. Most of the time we ignore this phase. This code ignores this phase
document.addEventListener('click',handleClick)
如果你想涉及到这一阶段,你必须这样写
document.addEventListener('click',handleClick,true)
当我们需要检测目标之外的点击时,我们就需要这个阶段。也许我们有一个打开的下拉菜单或者模态菜单如果用户点击了模态菜单或者下拉菜单之外的任何地方,我们想要关闭它
2-Target阶段
浏览器会查看被点击的按钮元素,如果按钮有事件处理程序,浏览器会调用它。
3-气泡阶段
与捕获阶段相反,在这个阶段,浏览器将从直接的父元素(在这种情况下是div元素)开始这个过程,然后它将访问body元素。这段代码将为气泡阶段设置事件处理程序
document.addEventListener('click',handleClick,false)
冒泡
Event propagate to the upto root element is **BUBBLING**.
捕捉
Event propagate from body(root) element to eventTriggered Element is **CAPTURING**.
推荐文章
- 如何使用jQuery检测页面的滚动位置
- if(key in object)或者if(object. hasownproperty (key)
- 一元加/数字(x)和parseFloat(x)之间的区别是什么?
- angularjs中的compile函数和link函数有什么区别
- 删除绑定中添加的事件监听器
- 很好的初学者教程socket.io?
- HtmlSpecialChars在JavaScript中等价于什么?
- React: 'Redirect'没有从' React -router-dom'中导出
- 如何在React中使用钩子强制组件重新渲染?
- 我如何使用Jest模拟JavaScript的“窗口”对象?
- 我如何等待一个承诺完成之前返回一个函数的变量?
- 在JavaScript中根据键值查找和删除数组中的对象
- 使嵌套JavaScript对象平放/不平放的最快方法
- 如何以及为什么'a'['toUpperCase']()在JavaScript工作?
- 有Grunt生成index.html不同的设置