Prevent execution of parent event handler

后端 未结 2 1888
暗喜
暗喜 2020-11-22 07:20

I have a tree of divs:

相关标签:
2条回答
  • 2020-11-22 07:37

    use

    event.stopPropagation()

    Stops the bubbling of an event to parent elements, preventing any parent handlers from being notified of the event.

    when click on b and c

    0 讨论(0)
  • 2020-11-22 07:44

    You can add a handler for the child that will prevent the click event from propagating up:

    function handler(event) {
        event.stopPropagation();
        // now do your stuff        
    }
    $('#a').add('#b').click(handler);
    

    This way clicks to '#b' will not propagate to '#a'. Neither will clicks to '#c' go to '#b', and hence not to '#a'.

    0 讨论(0)
提交回复
热议问题