How to prevent a click() event through a child div to a parent div in jQuery?

前端 未结 3 1686
谎友^
谎友^ 2021-01-01 09:06

Situation:

+ OPEN
相关标签:
3条回答
  • 2021-01-01 09:31
    $('.closeBtn').click(function (event){
       event.stopPropagation();
       //   ... your code here
       return false;
    });
    

    that should do the trick, so everytime you click on close it will not affect your header div,

    0 讨论(0)
  • 2021-01-01 09:41

    You need to stop the event from propagating up the DOM tree:

    $('.closeBtn').click(function (evt) {
        evt.stopPropagation();
    
        // Your code here
    });
    
    0 讨论(0)
  • 2021-01-01 09:44

    Why not hook open event with <div style="float: left;" class="headerTitle">+ OPEN</div> instead of parent div. Otherwise return false or stopPropagation from inner div click handler

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