Jquery StopPropagation在Firefox中无法工作 [英] Jquery StopPropagation not working in firefox

查看:141
本文介绍了Jquery StopPropagation在Firefox中无法工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码在Safari和Chrome上工作,但不在Firefox中。 firefox有StopPropagation()的问题吗?

  $(function(){
//安装程序下拉菜单
$('。dropdown-toggle')。dropdown();

//修复输入元素点击问题
$('。login-menu form')。函数(e){
alert(e.isPropagationStopped());
e.stopPropagation();
alert(e.isPropagationStopped());
});
});


解决方案

我不知道你的HTML看起来如何,但是在FireFox中可以使用 stopPropagation



我认为你的期望可能不正确。



根据 stopPropagation()


event.stopPropagation()

防止事件冒出DOM
树,防止任何括号


假设像这样的HTML,例如:

 < div id =parent> 
< div class =login-menu>点击任何地方< / div>
< / div>

此代码工作正常,点击事件不冒泡:

  $(function(){
//修正输入元素点击问题
$('。login-menu')。 (e){
alert(e.isPropagationStopped());
e.stopPropagation();
alert(e.isPropagationStopped());
});

$(#parent)点击(功能(e){
alert(parent received click);
});
});



演示 - 在FF中工作 - 不起泡事件



注释行 e.stopPropagation(); 在DEMO中,如预期的那样,事件冒泡起来,显示父母的警报。



你的预期行为是什么?



您没有指定预期会发生什么。



如果您希望事件的默认操作无效要触发,您可以使用 preventDefault()


event.preventDefault()

如果调用此方法,事件的默认操作将不要触发。


如果要同时覆盖这两个,请防止默认操作,并阻止事件冒泡,您可以调用两者或只需在方法结束时使用 return false



如果可以详细说明你正在尝试什么实现我们可以更具体的建议一个适合你的解决方案。


I have this code working on Safari and Chrome , but not in Firefox . Is firefox having a problem with StopPropagation() ?

$(function() {
    // Setup drop down menu
    $('.dropdown-toggle').dropdown();

    // Fix input element click problem
    $('.login-menu form').click(function(e) {
        alert(e.isPropagationStopped());
        e.stopPropagation();
        alert(e.isPropagationStopped());
    });
});​

解决方案

I don't know how your HTML looks but stopPropagation works fine in FireFox.

I think your expectations might be incorrect in what it does.

According to the documentation of stopPropagation()

event.stopPropagation()
Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.

Assuming HTML like this for example:

<div id="parent">
    <div class="login-menu">click anywhere</div>
</div>

This code works fine, the click event is not bubbling up:

$(function() {
    // Fix input element click problem
    $('.login-menu').click(function(e) {
        alert(e.isPropagationStopped());
        e.stopPropagation();
        alert(e.isPropagationStopped());
    });

    $("#parent").click(function(e){
        alert("parent received click");
    });
});​

DEMO - Works in FF - Does not bubble up event

Comment out the line e.stopPropagation(); in the DEMO and as expected the event is bubbled up, showing the alert from the parent.

What is your expected behaviour?

You have not specified what it is you expect to happen.

If for example you expected the default action of the event no to be triggered you can use preventDefault()

event.preventDefault()
If this method is called, the default action of the event will not be triggered.

If you want to cover both, prevent the default action and prevent the event from bubbling up you either call both or simply use return false at the end of your method.

If you could elaborate what it is you are trying to achieve we can be more specific in suggestion a solution which works for you.

这篇关于Jquery StopPropagation在Firefox中无法工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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