简体   繁体   English

我似乎无法停止将click事件传播到父元素

[英]I can't seem to stop propagation of click event to parent element

I cannot seam to stop propagation of the click signal to the parent element. 我无法缝合以停止将点击信号传播到父元素。 I have search and read through a lot of questions here but could not find a solution that works. 我在这里搜索并阅读了很多问题,但找不到有效的解决方案。 I am testing in Firefox. 我正在Firefox中进行测试。

//this is simply a div element
$("#" + self.id).css({ 
    "position" : "absolute"
});

$("#" + self.id ).append(
    '<div class="onoffswitch" id="'+ self.id + "_checkbox_d" + '">'+
        '<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="'+ self.id + "_checkbox" + 

        ( self.isOn ? '" checked>' : '">' ) +

        '<label class="onoffswitch-label" for="'+ self.id + "_checkbox" + '">'+
            '<div class="onoffswitch-inner"></div>'+
            '<div id="switch-mod' + self.id + '" class="onoffswitch-switch onoffswitch-switchon"></div>'+
        '</label>'+
    '</div>' );

$( "#" + self.id + "_checkbox_d" ).css({
    "position" : "absolute"
});

$('#' + self.id + "_checkbox").click( function( event ){

    if ($(this).is(':checked')) {

        $( "#switch-mod" + self.id  ).removeClass( "onoffswitch-switchoff" );
        $( "#switch-mod" + self.id  ).addClass(    "onoffswitch-switchon" );
    }
    else{

        $( "#switch-mod" + self.id  ).removeClass( "onoffswitch-switchon" );
        $( "#switch-mod" + self.id  ).addClass(    "onoffswitch-switchoff" );
    }

    event.stopPropagation();
    event.stopImmediatePropagation();
});

$("#" + self.id ).click( function() {

    $('html').trigger( "selection-panel-clicked", self.id );          
});

The last click handler for the parent element is called which I need to prevent. 父元素的最后单击处理程序被调用,我需要防止它。

EDIT 1: 编辑1:

I think the problem must be with the css for the checkbox/toggle switch. 我认为问题一定与复选框/切换开关的css有关。 I auto generated it from here, http://proto.io/freebies/onoff/ . 我是从这里http://proto.io/freebies/onoff/自动生成的。

I am finding that first the parent's click handler is called, then the child's is called, and then the parents is called again. 我发现首先调用父级的单击处理程序,然后调用子级的单击处理程序,然后再次调用父级。 Stop propigation doesn't change this. 停止传播不会改变这一点。 Return false results in just the parent then the child, but this also causes the toggle switch not to work. 仅在父项然后在子项中返回假结果,但这也会导致切换开关不起作用。

The switch includes these properties in css, 开关在CSS中包含这些属性,

.onoffswitch-switch {
    -moz-transition: all 0.3s ease-in 0s; -webkit-transition: all 0.3s ease-in 0s;
    -o-transition: all 0.3s ease-in 0s; transition: all 0.3s ease-in 0s;
}

.onoffswitch-inner {
    -moz-transition: margin 0.3s ease-in 0s; -webkit-transition: margin 0.3s ease-in 0s;
    -o-transition: margin 0.3s ease-in 0s; transition: margin 0.3s ease-in 0s;
}

Maybe this is relevant? 也许这是相关的?

EDIT 2: I ended up solving my problem taking the boxes out of the parent and putting them instead in the parents parent which doesn't respond to clicks and then positioning them based on the position of the panels they correspond to. 编辑2:我最终解决了我的问题,将盒子从父级盒子中取出,而不是将它们放在不响应单击的父级父级中,然后根据它们对应的面板的位置对其进行定位。 I would still like to figure out why I was having the issue though. 我仍然想弄清楚为什么我遇到了这个问题。

I don't have any problem with your code in Firefox 28 nor in Chrome 34.x. 我在Firefox 28或Chrome 34.x中的代码都没有问题。 Which version are you using? 您正在使用哪个版本?

Try removing 尝试移除

event.stopPropagation();
event.stopPropagation();

and just write 然后写

return false;

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

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