事件捕获与事件冒泡 [英] Event Capturing vs Event Bubbling

查看:156
本文介绍了事件捕获与事件冒泡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是希望得到普遍的共识,这是JS在冒泡和捕获之间更好的事件委派模式。

I just wanted to get the general consensus on which is a better mode of event delegation in JS between bubbling and capturing.

现在我理解这取决于特定的用例,人们可能想要使用捕获阶段而不是冒泡,反之亦然但是我想了解哪种委派模式对于大多数一般情况是优选的以及为什么(对我而言似乎冒泡模式)。

Now I understand that depending on a particular use-case, one might want to use capturing phase over bubbling or vice versa but I want to understand what delegation mode is preferred for most general cases and why (to me it seems bubbling mode).

或换句话说,W3C addEventListener实现背后的原因是支持冒泡模式。 [仅当您指定第3个参数及其true时才会启动捕获。但是,你可以忘记第三个参数和冒泡模式被踢了]

Or to put it in other words, is there a reason behind W3C addEventListener implementation to favor the bubbling mode. [capturing is initiated only when you specify the 3rd parameter and its true. However, you can forget that 3rd param and bubbling mode is kicked in]

我查看了JQuery的绑定函数来得到我的问题的答案,它似乎没有t甚至支持捕获阶段的事件(在我看来因为IE不支持捕获模式)。

I looked up the JQuery's bind function to get an answer to my question and it seems it doesn't even support events in capture phase (it seems to me because of IE not support capturing mode).

所以看起来像冒泡模式是默认选择,但为什么?

So looks like bubbling mode is the default choice but why?

推荐答案

过去这是一个平台问题,Internet Explorer有一个冒泡的模型,而Netscape更多的是捕获(但同时支持) 。

In the past it was a platform issue, Internet Explorer had a bubbling model, and Netscape was more about capturing (yet supported both).

W3C模型要求你能够选择你想要的那个。

The W3C model calls for you be able to choose which one you want.

我认为冒泡是更受欢迎的原因是,正如所述,有些平台只支持冒泡...... 它有点像默认模式。

I think bubbling is more popular because, as stated there are some platforms that only support bubbling...and it sort of makes sense as a "default" mode.

您选择哪一个主要是您正在做的事情和对您有意义的产品。

Which one you choose is largely a product of what you are doing and what makes sense to you.

这篇关于事件捕获与事件冒泡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆