jquery Event.stopPropagation() 似乎不起作用

标签 jquery events

我完全错过了它应该做什么吗?我希望如果我在某个事件上调用 stopPropagation() ,该事件的处理程序将不会在祖先元素上触发,但下面的示例不会以这种方式工作(至少在 FireFox 3 中)..

<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 event handling 上的文档.

引用上面的引用文献:

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() 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1122375/

相关文章:

javascript - 在 onkeyup 模拟按钮点击

php - 点击加载更多 WordPress 帖子

javascript - 当执行另一个函数时,Jquery 触发函数

asp.net - 如何动态创建一个asp按钮并向其添加事件

javascript - 创建 jQuery 插件时如何使用 ES6 模块?

javascript - 在 sails js 中处理事件

只有一个 Activity 和多个 fragment 的 Android

javascript - 如何让jquery选择选项

jquery - CSS导航悬停效果

jquery - 如何防止当 select 的 HTML 更改时触发 JQuery 更改事件?