how to prevent the click event of the parent when its children is being dragged

落爺英雄遲暮 提交于 2021-01-28 10:14:05

问题


Here is a sample code:

$('.bmc-div').click(function(e){
    e.stopPropagation();
    // console.log(e.target);
    if ($(e.target).hasClass('my-sticky')) {

    } else {
        canvas.add_sticky($(this));
    }
});

$('.bmc-div .sticky_container').sortable({
    cursor: 'move',
    revert: true,  
    helper: 'clone',
    // handle: ".element-handler",
    connectWith: '.bmc-div .sticky_container',
    scroll: true,
    cancel: null,
    opacity: 0.7,
    // axis: 'y',
    items: ".my-sticky",
    placeholder : "sticky-placeholder",
    containment: "#main",
    zIndex: 9999,
    start: function(event, ui) {
        event.stopPropagation();
    },
    stop: function(event, ui) {
        event.stopPropagation();
    },
    update: function(event, ui) {
        // update_sortable_position();
    }
}).disableSelection();

Here is the HTML Code for the question:

 <div id="bmc-rs" class="col-sm-12 bmc-div">
        <div class='bmc-row'>
             <p style='margin: 0px;'><span class="grid-label">Revenue Streams              </span><span class="pull-right"><i class="fa fa-briefcase"></i></span></p>
              </div>
              <div class="sticky_container">
                <div class="my-sticky" style="display: block;">Sample Sticky</div>
              </div>
            </div>

The problem is that the child element will be dragged but when stopped, it will still trigger the click event of the parent container.


回答1:


You need to stop propagation on your draggable element's mousedown event:

$('.sticky_container').mousedown(function (event) {
    event.stopPropagation();
});


来源:https://stackoverflow.com/questions/29935195/how-to-prevent-the-click-event-of-the-parent-when-its-children-is-being-dragged

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!