在事件捕获阶段(不是事件冒泡)绑定 jQuery 处理程序

发布于 2024-09-13 21:43:42 字数 435 浏览 1 评论 0原文

我正在寻求对模糊/焦点事件实施事件委托,其方式与 怪异模式。正如 TFA 中所解释的,模糊和焦点事件不会冒泡,因此您不能在冒泡阶段对它们使用事件委托,但您可以在捕获阶段捕获它们(man,javascript events很奇怪)。

无论如何,据我所知,jQuery 事件都适用于冒泡阶段,或者至少是我一直使用它们的地方。我在 jQuery 文档中看不到有关这种方式或另一种方式的任何信息,并且默认情况下使用 $('#foo').blur(blurHandler) 之类的东西似乎无法捕获它。

为了保持一致性,我宁愿坚持使用 jQuery;有什么办法可以做到这一点吗?

I'm looking to implement event delegation on blur/focus events, in a similar way to that suggested on quirksmode. As explained in TFA, blur and focus events don't bubble, so you can't use event delegation with them in the bubbling phase, but you can grab them in the capture phase (man, javascript events are weird).

Anyhow, as far as I can see, jQuery events all apply to the bubbling phase, or at least that's where I've always used them. I can't see any information on this one way or the other in the jQuery docs, and by default using something like $('#foo').blur(blurHandler) doesn't seem to capture it.

I would prefer to stick to using jQuery for consistency; is there any way to do this?

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

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

发布评论

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

评论(1

画尸师 2024-09-20 21:43:42

如果您希望容器在后代 获得焦点或模糊时触发处理程序,请尝试使用 .focusin().focusout()。

来自文档:

当元素或其内部的任何元素获得焦点时,focusin 事件将发送到该元素。这与焦点事件不同,它支持检测父元素上的焦点事件。

当某个元素或其内部的任何元素失去焦点时,将向该元素发送 focusout 事件。这与模糊事件不同,它支持检测父元素失去焦点(换句话说,它支持事件冒泡)。

If you're saying that you want a container to trigger a handler when a descendant <input> gets focus or blur, try using .focusin() and .focusout().

From the docs:

The focusin event is sent to an element when it, or any element inside of it, gains focus. This is distinct from the focus event in that it supports detecting the focus event on parent elements.

The focusout event is sent to an element when it, or any element inside of it, loses focus. This is distinct from the blur event in that it supports detecting the loss of focus from parent elements (in other words, it supports event bubbling).

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