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

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

E.g

I am not fire when click me
$(\'.container\').click(function(){
3条回答
  •  时光说笑
    2021-01-12 02:50

    As a more general solution, you should check e.target in container's click event handler.

    $('.container').click(function(e) {
       // If you want to ignore clicks to anything inside the `.container` div:
       if (!$(e.target).hasClass('container')) return;
       // or, if you only want the `.inside` div to not fire the event,
       if ($(e.target).hasClass('inside')) return;
    
       // container do something here
    });
    

    This way you're not preventing propagation of the event, which would break bound live handlers.

提交回复
热议问题