How to cancel click event of container div trigger when click elements which inside container in JQuery?

后端 未结 3 1890
终归单人心
终归单人心 2021-01-12 02:32

E.g

I am not fire when click me
$(\'.container\').click(function(){
3条回答
  •  无人共我
    2021-01-12 02:54

    Add a click event handler to inside as follows:

    $('.inside').click(function(event) {
        // do anything you might want with the click for inside
        return false; // prevents click event from bubbling up the DOM hierarchy
    });
    

    Alternatively, event.stopPropagation() also prevents bubbling like so:

    $('.inside').click(function(event) {
        // do anything you might want with the click for inside
        event.stopPropagation(); // prevents click event from bubbling up the DOM hierarchy
    });
    

    This article explains event bubbling.

提交回复
热议问题