How to ONLY trigger parent click event when a child is clicked

后端 未结 5 703
时光说笑
时光说笑 2020-11-29 18:17

Both child and parent are clickable (child could be a link or div with jQuery click events). When I click on child, how do I only trigger parent click event but not the chil

5条回答
  •  囚心锁ツ
    2020-11-29 18:58

    Another option for this that may be useful in certain circumstances when you know that none of the child elements are interactive is to set pointer-events: none in your css (link). I usually apply it to all child elements of the element on which I want to capture interaction. Like this:

    #parentDiv * {
        pointer-events: none
    }
    

    Note the *, declaring that the rule applies to all children of the parentDiv.

提交回复
热议问题