简体   繁体   English

为什么event.stopPropagation也可以阻止我的Bootstrap崩溃?

[英]Why is event.stopPropagation also stopping my Bootstrap Collapse?

I have a list item ( #planAdminMenuItem ) that has an onclick attribute. 我有一个具有onclick属性的列表项( #planAdminMenuItem )。 This list item has an icon inside of it ( .spinner ) that will collapse #collapseExample . 此列表项内部有一个图标( .spinner ),它将折叠#collapseExample Whenever the .spinner is clicked, I want it to run bootstrap collapse only. 每当单击.spinner ,我都希望它仅运行bootstrap折叠。 I do not want it to run drawPlanAdmin function. 我不希望它运行drawPlanAdmin函数。 I have tried adding event.stopPropagation to my toggleSpinnerLeftMenu function, but whenever I do that, it also stops the bootstrap collapse. 我曾尝试将event.stopPropagation添加到我的toggleSpinnerLeftMenu函数中,但是每当这样做时,它也会阻止引导崩溃。 The parent click is blocked, but so is bootstrap collapse. 父级单击被阻止,但引导崩溃也被阻止。

THE PHP & HTML CODE PHP和HTML代码

<ul>            
    <li id="planAdminMenuItem" onclick="plan.drawPlanAdmin();"> 
        Book Plan
        <span class="icn icn-chevron-down spinner" onclick="ui.toggleSpinnerLeftMenu(this,event);" data-toggle="collapse" data-target="#collapseExample" data-aria-expanded="true" aria-controls="collapseExample"></span>
    </li>
    <!-- the collapsable area -->               
    <li id="collapseExample" class="collapse in">
        <ul>
            <li onclick="plan.drawRunListAdmin();">
                Run List View
            </li>
            <li onclick="plan.drawLadderAdmin();"> 
                Ladder View
            </li>               
        </ul>
    </li>
</ul>

THE JS CODE JS代码

toggleSpinnerLeftMenu:function(el,event){
    el = jQuery(el);

    if(el.hasClass('icn-chevron-up')){
        el.addClass('icn-chevron-down');
        el.removeClass('icn-chevron-up');
    }else if(el.hasClass('icn-chevron-down')){
        el.addClass('icn-chevron-up');
        el.removeClass('icn-chevron-down');

    }
    event.stopPropagation(); //why is this stopping the collapse also?
},

stopPropagation is doing exactly what it is meant to do. stopPropagation确实正在执行其应做的工作。

If you want the parent element to be propagated by the click on the inner element then simply don't do event.stopPropagation at all. 如果您希望通过单击内部元素来传播父元素,则根本不执行event.stopPropagation

Though for some reasons if you need to have that then my suggestion is: call the function like 虽然由于某些原因,如果您需要该功能,那么我的建议是:像

toggleSpinnerLeftMenu:function(el,event){
    el = jQuery(el);

    if(el.hasClass('icn-chevron-up')){
        el.addClass('icn-chevron-down');
        el.removeClass('icn-chevron-up');
    }else if(el.hasClass('icn-chevron-down')){
        el.addClass('icn-chevron-up');
        el.removeClass('icn-chevron-down');

    }
    plan.drawPlanAdmin(); // Call the function inside of the child element's click handler.
    event.stopPropagation(); //why is this stopping the collapse also?
},

Update: Since you described the issue more clearly in the comment, which has a solution completely south of what I've written above, I am updating with the new content that may be able to help. 更新:由于您在评论中更清楚地描述了该问题,并且该解决方案完全超出了我上面写的内容,因此,我正在更新可能有帮助的新内容。

Instead of attaching two event handlers, one using an inline onClick attribute and another using Bootstrap's data-collapse , use one: 而不是附加两个事件处理程序,一个使用内联onClick属性,另一个使用Bootstrap的data-collapse ,使用一个:

$(".spinner").on("click", function(event) { // tip: Give a better id or class name 
    $('#collapseExample').collapse({toggle: true});
    ui.toggleSpinnerLeftMenu(this, event);
}); 

This is the general idea of doing this, you may still have to make some adjustments to your method calls to fit it in. 这是执行此操作的一般想法,您可能仍需要对方法调用进行一些调整以使其适合。

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

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