jquery Event.stopPropagation() 似乎不起作用 [英] jquery Event.stopPropagation() seems not to work

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

问题描述

我是否完全错过了它应该做的事情?我希望如果我在事件上调用 stopPropagation(),则该事件的处理程序不会在祖先元素上被触发,但下面的示例不会那样工作(至少在 FireFox 3 中)..

Am I totally missing what this is supposed to do? I expect that if I call stopPropagation() on an event, handlers for that event won't get triggered on ancestor elements, but the example below isn't working that way (in FireFox 3 at least)..

<script type="text/javascript">
    $("input").live("click", function(event){
        console.log("input click handler called")
        event.stopPropagation()
    });

    $("body").live("click", function(event){
        console.log("body was click handler called. event.isPropagationStopped() returns: " + event.isPropagationStopped());
    })

</script>

 ...

<body>
    <input type="text" >
</body>

推荐答案

实时事件不遵循相同的事件冒泡规则.请参阅有关实时事件处理的文档.

Live events don't follow the same event bubbling rules. See the documentation on live event handling.

引用以上参考:

现场活动不会在传统方式,不能停止使用 stopPropagation 或停止立即传播.例如,以两个点击事件为例 -一个绑定到li",另一个绑定到li a".如果内部发生点击锚点两个事件都会被触发.这是因为当一个$("li").bind("click", fn);被绑定你实际上是在说每当单击事件发生在 LI 元素上 -或在 LI 元素内 - 触发此单击事件." 进一步停止处理实时事件,fn 必须返回假.

Live events do not bubble in the traditional manner and cannot be stopped using stopPropagation or stopImmediatePropagation. For example, take the case of two click events - one bound to "li" and another "li a". Should a click occur on the inner anchor BOTH events will be triggered. This is because when a $("li").bind("click", fn); is bound you're actually saying "Whenever a click event occurs on an LI element - or inside an LI element - trigger this click event." To stop further processing for a live event, fn must return false.

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

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