简体   繁体   English

javascript stopPropagation()阻止函数触发

[英]javascript stopPropagation() prevents function from firing

I have a child div with class body_left_details which resides in a parent div with class body_left_bar . 我有一个类body_left_details的子div,它位于一个父类body_left_bar div中。 Both divs have an onclick event attached to them, but I want to prevent the bubbling of body_left_bar when you click on body_left_details . 两个div都附加了一个onclick事件,但是我想防止在单击body_left_bar时冒泡body_left_details Here's the relevant code: 以下是相关代码:

$(function() {
    $('.body_left_bar').click(function() {
        body_right_load('parent');
    })
    $('.body_left_details').click(function(e) {
        e.stopPropagation();
        body_right_load('child');
    })
})
function body_right_load(str) {
    alert(str);
}​

Simple enough. 很简单。 But why is it that when I click on body_left_details , body_right_load('child') is not called? 但是为什么当我单击body_left_details ,body_right_load('child')没有被调用? I tested with an alert() directly after e.stopPropagation(), and it worked fine. 我在e.stopPropagation()之后直接使用alert()进行了测试,并且工作正常。 Are external functions not allowed after stopPropagation? stopPropagation之后是否不允许使用外部功能? Would that be considered bubbling? 那会被认为是冒泡的吗?

Testing on Windows 7 wamp in Chrome. 在Windows 7 Wamp中使用Chrome进行测试。 Any insight appreciated! 任何见解表示赞赏!

EDIT: Relevant jsfiddle . 编辑: 相关的jsfiddle Clicking on parent div will result in alert, but clicking on "Read about it" yields nothing. 单击父div将导致警报,但是单击“阅读有关内容”不会产生任何警告。

$(function() {
    $('.body_left_bar').on('click', function() {
        body_right_load('parent');
    })
    $('.body_left_details').on('click', function(e) {
        e.stopImmediatePropagation();
        body_right_load('child');
    })
})
function body_right_load(str) {
    alert(str);
}​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM