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

后端 未结 22 1373
旧巷少年郎
旧巷少年郎 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:30

    If you do not intend to interact with the inner element/s in any case, then a CSS solution might be useful for you.

    Just set the inner element/s to pointer-events: none

    in your case:

    .clickable > a {
        pointer-events: none;
    }
    

    or to target all inner elements generally:

    .clickable * {
        pointer-events: none;
    }
    

    This easy hack saved me a lot of time while developing with ReactJS

    Browser support could be found here: http://caniuse.com/#feat=pointer-events

提交回复
热议问题