最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - stop parent onClick when child is clicked (React or vanilla JS) - Stack Overflow

matteradmin5PV0评论

I have a sticky bar that I would like to let users close out by having an X in the right side with position: absolute. However, when clicked, it also fires off the "Share to Facebook" event. How do I stop this from happening?

JS File

return (
    <div
      className={'ShareBar'}
      onClick={()=>console.log('sharing')}>
        <span className={'ShareCTA'}>
          {facebook.svg} Share on Facebook &trade;
          <span
            className={'CloseButton'}
            handleClick={function (e) { e.stopPropagation()/*attempted fix*/; console.log('closing'); return; }}>
              X
          </span>
        </span>
    </div>
  );

CSS File (it's a sass file so no semicolons and stuff

.ShareBar
  position fixed
  bottom 0
  z-index 9999
  display flex
  justify-content flex-end
  align-items center
  height 48px
  cursor pointer
  width 100%
  ...

  .ShareCTA
    width 100%
    position relative
    display flex
    justify-content center
    align-items center
    font-size 20px
    ...
    svg
      height 20px

  .CloseButton
    position absolute
    z-index 99999
    right 10px
    border-radius 50%
    height 40px
    width 40px
    display flex
    justify-content center
    align-items center
    ...

I have a sticky bar that I would like to let users close out by having an X in the right side with position: absolute. However, when clicked, it also fires off the "Share to Facebook" event. How do I stop this from happening?

JS File

return (
    <div
      className={'ShareBar'}
      onClick={()=>console.log('sharing')}>
        <span className={'ShareCTA'}>
          {facebook.svg} Share on Facebook &trade;
          <span
            className={'CloseButton'}
            handleClick={function (e) { e.stopPropagation()/*attempted fix*/; console.log('closing'); return; }}>
              X
          </span>
        </span>
    </div>
  );

CSS File (it's a sass file so no semicolons and stuff

.ShareBar
  position fixed
  bottom 0
  z-index 9999
  display flex
  justify-content flex-end
  align-items center
  height 48px
  cursor pointer
  width 100%
  ...

  .ShareCTA
    width 100%
    position relative
    display flex
    justify-content center
    align-items center
    font-size 20px
    ...
    svg
      height 20px

  .CloseButton
    position absolute
    z-index 99999
    right 10px
    border-radius 50%
    height 40px
    width 40px
    display flex
    justify-content center
    align-items center
    ...
Share Improve this question asked Jul 18, 2018 at 22:08 Kevin DanikowskiKevin Danikowski 5,2268 gold badges50 silver badges91 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

stopPropagation will work like you have written, but the event handler for click events is called onClick, not handleClick:

<span
  className={'CloseButton'}
  onClick={e => e.stopPropagation()}
>
  X
</span>

Wrap the Share to Facebook text in a span and attach the onClick handler to that.

You shouldn't nest an element with an onClick inside another with an onClick.

Post a comment

comment list (0)

  1. No comments so far