How do I prevent a parent's onclick event from firing when a child anchor is clicked?

后端 未结 22 1367
旧巷少年郎
旧巷少年郎 2020-11-22 00:37

I\'m currently using jQuery to make a div clickable and in this div I also have anchors. The problem I\'m running into is that when I click on an anchor both click events ar

相关标签:
22条回答
  • 2020-11-22 01:11

    If a child element is clicked, then the event bubbles up to the parent and event.target !== event.currentTarget.

    So in your function, you can check this and return early, i.e.:

    var url = $("#clickable a").attr("href");
    $("#clickable").click(function(event) {
        if ( event.target !== event.currentTarget ){
            // user clicked on a child and we ignore that
            return;
        }
        window.location = url;
        return true;
    })
    
    0 讨论(0)
  • 2020-11-22 01:12

    add a as follows:

    <a href="http://foo.com" onclick="return false;">....</a>
    

    or return false; from click handler for #clickable like:

      $("#clickable").click(function() {
            var url = $("#clickable a").attr("href");
            window.location = url;
            return false;
       });
    
    0 讨论(0)
  • 2020-11-22 01:17

    e.stopPropagation() is a correct solution, but in case you don't want to attach any event handler to your inner anchor, you can simply attach this handler to your outer div:

    e => { e.target === e.currentTarget && window.location = URL; }
    
    0 讨论(0)
  • 2020-11-22 01:21

    Events bubble to the highest point in the DOM at which a click event has been attached. So in your example, even if you didn't have any other explicitly clickable elements in the div, every child element of the div would bubble their click event up the DOM to until the DIV's click event handler catches it.

    There are two solutions to this is to check to see who actually originated the event. jQuery passes an eventargs object along with the event:

    $("#clickable").click(function(e) {
        var senderElement = e.target;
        // Check if sender is the <div> element e.g.
        // if($(e.target).is("div")) {
        window.location = url;
        return true;
    });
    

    You can also attach a click event handler to your links which tell them to stop event bubbling after their own handler executes:

    $("#clickable a").click(function(e) {
       // Do something
       e.stopPropagation();
    });
    
    0 讨论(0)
  • 2020-11-22 01:23

    Use stopPropagation method, see an example:

    $("#clickable a").click(function(e) {
       e.stopPropagation();
    });
    

    As said by jQuery Docs:

    stopPropagation method prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.

    Keep in mind that it does not prevent others listeners to handle this event(ex. more than one click handler for a button), if it is not the desired effect, you must use stopImmediatePropagation instead.

    0 讨论(0)
  • 2020-11-22 01:23

    If you have multiple elements in the clickable div, you should do this:

    $('#clickable *').click(function(e){ e.stopPropagation(); });
    
    0 讨论(0)
提交回复
热议问题