Prevent click from child firing parent click event

后端 未结 4 703
抹茶落季
抹茶落季 2020-12-09 11:45

I have a selector that binds a click event which will remove the popup. However, I only want the selector to handle the click, instead of the children of the selector to be

相关标签:
4条回答
  • 2020-12-09 12:10

    try:

    e.stopPropagation();
    return false; 
    

    in your event handler

    0 讨论(0)
  • 2020-12-09 12:17
    $('.popup-content').bind('click', function(e)
    {
        e.stopPropagation();
    });
    

    or

    $('.popup-content').bind('click', function(e)
    {
        return false;
    });
    

    In your case - it's same, but sometimes you can't do such thing without e.stopPropagation(). For example:

    $('.popup-content a').bind('click', function(e)
    {
        e.stopPropagation();
        return confirm("Are you sure?");
    });
    
    0 讨论(0)
  • 2020-12-09 12:30
    {if(e.target == this ){ return;}});
    
    0 讨论(0)
  • 2020-12-09 12:34

    In your event handler for #popup check if e.target == this. i.e.:

    $('#popup').bind('click', function(e) {
        if(e.target == this) $(this).remove();
    });
    

    Doing this is much easier than binding extra click handlers to all the children.

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