jquery firefox stopPropagation()

Riz*_*Riz 5 firefox jquery event-handling

I'm binding two event handlers to an input field on 'keydown'. If the enter key has been pressed, the first event handler needs to stop the propagation of the event so that it doesn't hit the second eventhandler. I'm doing it like so:

if (jQuery.browser.msie) {
                    event.cancelBubble = true;
                } else {
                    event.stopPropagation();
                }
Run Code Online (Sandbox Code Playgroud)

now this alone doesn't stop the event propagation either in IE or Firefox. It hits the first event handler, and then hits the second event handler as well. However, in the second event handler, I can actually check if (e.cancelBubble) in case of IE. Is there a way to check the same with Firefox?

Dou*_*ner 13

只需删除您的IE测试并使用此:

event.stopImmediatePropagation();
Run Code Online (Sandbox Code Playgroud)

这将阻止其他事件在两个浏览器中触发.

event.stopPropagation() 将阻止事件冒泡,但不会阻止同一对象的其他事件处理程序触发.

要回答你的另一个问题,如果你刚刚使用过,event.stopPropagation()你可以检查event.isPropagationStopped()第二个处理程序.

建议:作为一般规则,jQuery完全抽象所有浏览器行为,为功能提供单一接口.如果你if(jQuery.browser.msie)在运行jQuery函数之前发现自己正在运行,那么可能有一种更好的方法来运行它可以跨浏览器工作.而且,当您确实需要测试时,您应该使用jQuery.support测试功能而不是特定的浏览器嗅探.

  • 哇男人,这就像一个魅力!我甚至不必使用isPropagationStopped(),因为stopImmediatePropagation()实际上工作!非常感谢. (2认同)