为什么要“点击”、“委托”?和“现场”使用 jQuery 在事件冒泡方面表现不同?

发布于 2024-12-23 05:39:45 字数 591 浏览 3 评论 0 原文

我编写了一个示例 JavaScript 程序来演示 jQuery 函数 clickdelegatelive 令人困惑的不同事件冒泡行为。

这是演示页面

对于每个函数,都有一个包装器和包装器中的点击链接,这两个函数都注册了点击事件函数。

我注意到

  1. livedelegate 中使用 stopPropagation 并不能阻止
  2. livedelegate< 中的 事件冒泡/code>,首先触发包装器中的单击事件,然后触发点击链接。但是,对于 click 函数来说,这个顺序是相反的。

谁能解释一下这两种现象?

该示例使用 jQuery 1.6.4,但您可以调整版本。

I wrote a sample JavaScript program to demostrate the confusingly different event bubbling behavior of jQuery's function, click, delegate and live.

Here is the demo page.

For each function, there is a wrapper and a click link in the wrapper, both of which are registered with click event functions.

I notice that

  1. Using stopPropagation in live and delegate does not prevent event bubbling
  2. In live and delegate, the click event in the wrapper is fired first and the click link next. However, this order is reversed for click function.

Can anyone explain these two phenomenon?

The sample is using jQuery 1.6.4, but you can adjust the version.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天冷不及心凉 2024-12-30 05:39:45

首先,live 已被弃用,取而代之的是 on委托 。我会立即停止使用 live,而不是稍后。上面链接的文档实际上内容非常丰富,并且很好地解释了这些功能的工作原理。

delegate 和 live 都依赖冒泡。这就是它们与点击的不同之处。这件事已经浮出水面。委托优于实时,因为您告诉它监视特定的父级,而实时则一直冒泡到文档级别。非常贵。

jQuery 文档值得在这里引用:

由于 .live() 方法在事件传播到
在文档的顶部,不可能停止传播
现场活动。类似地,由 .delegate() 处理的事件将传播
给他们被委托的部门;绑定的事件处理程序
DOM 树中位于其下方的所有元素都已被执行
调用委托事件处理程序时。这些处理者,
因此,可能会阻止委托处理程序触发
调用 event.stopPropagation() 或返回 false。

To start, live is deprecated in favor of on and delegate in jQuery 1.7+. I would stop using live now rather than later. The docs linked above are actually very informative and do a great job of explaining how these functions work.

Delegate and live both rely on bubbling. Which is how they differ from click. The event has bubbled up to them. Delegate is superior to live because you tell it to monitor a specific parent whereas live bubbles all the way up to the document level. Very expensive.

The jQuery documentation is worth quoting here:

Since the .live() method handles events once they have propagated to
the top of the document, it is not possible to stop propagation of
live events. Similarly, events handled by .delegate() will propagate
to the elements to which they are delegated; event handlers bound on
any elements below it in the DOM tree will already have been executed
by the time the delegated event handler is called. These handlers,
therefore, may prevent the delegated handler from triggering by
calling event.stopPropagation() or returning false.

泡沫很甜 2024-12-30 05:39:45

如果您了解 live 和 delegate 的实现,那么您就会理解这种差异的原因。 Live 和 delegate 在某种程度上不附加到元素,因为元素在绑定时不存在,它们分别附加到根或特定父级,因此存在差异。他们利用事件冒泡,以便当事件从子级冒泡到根或特定父级时,它会为新添加的元素执行此博客条目以获取有关此内容的详细视图

: alfajango.com/blog/the-difference- Between-jquerys-bind-live-and-delegate/" rel="nofollow">http://www.alfajango.com/blog/the-difference- Between-jquerys-bind-live-and-delegate/

If you understand the implementation of live and delegate then you would understand the reason for this difference. Live and delegate in a way are not attached to the element because the element is not there at the time of binding they are attached to either the root or a specific parent respectively and hence the difference. And they make use of event bubbling so that when event bubbles from child to the root or a specific parent it executes for the newly added element see this blog entry for a detailed view around this:

http://www.alfajango.com/blog/the-difference-between-jquerys-bind-live-and-delegate/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文