简体   繁体   English

聚焦和模糊jQuery事件而不是冒泡

[英]Focus and blur jQuery events not bubbling

With the following html structure: 使用以下html结构:

<div>
<form><span><input></span></form>
</div>
<p>

and the following jquery code: 和以下jquery代码:

$('form').on("focus", function(event) {
    $("p").append("focus no delegation<br>");
})

Why doesn't the focus event ever reach my event handler? 为什么焦点事件没有到达我的事件处理程序? Binding the event with a selector parameter works fine: 使用选择器参数绑定事件可以正常工作:

$('form').on("focus", "input", function(event) {
    $("p").append("focus delegation<br>");
})

Event the next snippet works so the focus event does in fact bubble... 活动下一个片段的作用,所以焦点事件实际上泡沫......

$('form').on("focus", "span", function(event) {
    $("p").append("focus delegation<br>");
})

Both forms work with click and change events: 两种形式都适用于点击和更改事件:

$('form').on("click", function(event) {
    $("p").append("click no delegation<br>");
})
$('form').on("click", "input", function(event) {
    $("p").append("click delegation<br>");
})

The only note I found about the focus event's bubbling is relative to older jQuery versions which I don't use. 我发现关于焦点事件冒泡的唯一注意事项是相对于我不使用的旧jQuery版本。 See it in action here 这里看到它

edit 1 编辑1

Well this is confusing... According to jQuery's focus doc: 这很令人困惑......根据jQuery的焦点文档:

The focus event does not bubble in Internet Explorer. 焦点事件不会在Internet Explorer中冒泡。 Therefore, scripts that rely on event delegation with the focus event will not work consistently across browsers. 因此,依赖于焦点事件的事件委派的脚本将无法跨浏览器一致地工作。 As of version 1.4.2, however, jQuery works around this limitation by mapping focus to the focusin event in its event delegation methods, .live() and .delegate(). 但是,从版本1.4.2开始,jQuery通过将焦点映射到其事件委托方法.live()和.delegate()中的focusin事件来解决此限制。

And according to jQuery's focusin doc: 根据jQuery的焦点文档:

The focusin event is sent to an element when it, or any element inside of it, gains focus. 当focusin或其中的任何元素获得焦点时,focusin事件将被发送到元素。 This is distinct from the focus event in that it supports detecting the focus event on parent elements (in other words, it supports event bubbling). 这与焦点事件的不同之处在于它支持在父元素上检测焦点事件(换句话说,它支持事件冒泡)。

Is it too late for me or does one contradict the other? 对我来说为时已晚还是与另一方相矛盾?

As Ohgodwhy pointed out, using focusin instead of focus does work. 正如Ohgodwhy指出的那样,使用focusin而不是焦点确实有效。

However, I can't understand how the following code can work if the "focus" event does not bubble: 但是,如果“焦点”事件没有冒泡,我无法理解以下代码如何工作:

$('form').on("focus", "span", function(event) {
    $("p").append("focus delegation<br>");
})

If a span child of the form receives the "focus" event, it means that the event bubbled from the input to the span. 如果表单的span子句收到“焦点”事件,则表示事件从输入冒泡到span。 Even this works! 即便如此!

$('div').on("focus", "form", function(event) {
    $("p").append("focus delegation<br>");
})

So... using "focusin" does fix the problem, but I'm not fully satisfied by this workaround. 所以...使用“focusin”确实解决了这个问题,但我对这个解决方法并不完全满意。 If anybody has a better answer, I'll happily accept it. 如果有人有更好的答案,我会高兴地接受它。

Yes, it appears the jQuery docs are misleading. 是的,似乎jQuery文档具有误导性。 I believe the documentation on focus neglected to mention that this was for the elements that aren't involved in user input (@Ohgodwhy listed them above in your question's comments). 我认为关于focus的文档忽略了提到这是针对不涉及用户输入的元素(@Ohgodwhy在你的问题的评论中将它们列在上面)。

I imagine it has something to do with the browser's need to trap the cursor in the input element that has focus , so that it can accept input from the keyboard. 我想这与浏览器需要将光标捕获到具有focus的输入元素有关,因此它可以接受来自键盘的输入。 In other words, if jQuery allowed it to bubble, then you would never be given the chance to type in the input field. 换句话说,如果jQuery允许它冒泡,那么你永远不会有机会输入输入字段。

Either way you'll never get a form to accept a focus event unless you first put a tabindex on it: http://jsfiddle.net/qxLqP/ but if an input based field gets focus first, it will never bubble. 无论哪种方式,你永远不会得到一个表单来接受focus事件,除非你首先在它上面添加一个tabindexhttp//jsfiddle.net/qxLqP/但是如果基于输入的字段首先获得焦点,它将永远不会冒泡。 By default, the form element doesn't have a tabindex , and you can't set focus to something that doesn't have a tabindex . 默认情况下, form元素没有tabindex ,并且您无法将焦点设置为没有tabindex

I'd just accept @Ohgodwhy's solution of using focusin and then go let the jQuery team know about their confusing documentation. 我只是接受@使用Ohgodwhy的解决方案focusin ,然后去让jQuery开发团队了解他们的困惑文档。

Jquery has a cross browser function which takes care of focus delegations. Jquery有一个跨浏览器功能,负责焦点授权。

Chrome,safari and opera support an event called "DOMFocusIn" which actually delegates. Chrome,safari和opera支持名为“DOMFocusIn”的事件,该事件实际上是代表。

IE supports an event "focusin" for delegating focus. IE支持用于委派焦点的事件“focusin”。

Firefox supports "focus" only on capturing phase and not bubbling phase. Firefox仅支持捕获阶段而非冒泡阶段的“焦点”。

jQuery made a cross browser event which is "focus" which actually delegates. jQuery创建了一个跨浏览器事件,它实际上是委托的“焦点”。

I hope this answers all your doubts. 我希望这能解决你所有的疑虑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM