jquery firefox stopPropagation() [英] jquery firefox stopPropagation()

查看:99
本文介绍了jquery firefox stopPropagation()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将两个事件处理程序绑定到'keydown'的输入字段。如果按下Enter键,则第一个事件处理程序需要停止事件的传播,以使其不会击中第二个事件处理程序。我这样做:

  if(jQuery.browser.msie){
event.cancelBubble = true ;
} else {
event.stopPropagation();
}

现在这一点不会阻止IE或Firefox中的事件传播。它会触发第一个事件处理程序,然后再次触发第二个事件处理程序。然而,在第二个事件处理程序中,我可以检查if(e.cancelBubble)在IE的情况下。有没有办法检查与Firefox相同?

解决方案

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

  event.stopImmediatePropagation(); 

这两个浏览器中的其他事件将不会触发。



event.stopPropagation()将保持事件冒泡,但不会为同一个对象的其他事件处理程序保持触发。



要回答您的其他问题,如果您刚刚使用 event.stopPropagation(),则可以检查 event.isPropagationStopped()



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


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();
                }

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?

解决方案

Just remove your test for IE and use this:

event.stopImmediatePropagation();

Which will keep other events from firing in both browsers.

event.stopPropagation() will keep events from bubbling, but won't keep other event handlers for the same object from firing.

To answer your other question, if you just used event.stopPropagation() you could check event.isPropagationStopped() in the second handler.

Suggestion: as a general rule jQuery fully abstracts all browsers behavior to provide a single interface to the functionality. If you find yourself running if(jQuery.browser.msie) before running a jQuery function, there is probably a better way to run it that will work cross browser. And, when you do need to test, you should use jQuery.support to test for functionality not specific browser sniffing.

这篇关于jquery firefox stopPropagation()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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