Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
700 views
in Technique[技术] by (71.8m points)

react 怎么阻止点击子元素触发父元素事件

image.png
怎么点击Input的onchage事件时候只触发当前事件 不触发父元素上的onclick事件
阻止冒泡都不行

  //点击选中按钮
  checkThis=(item,e)=>{
    console.log(e)
    e.stopPropagation();
    e.nativeEvent.stopImmediatePropagation();
    const eventType = e.type; // => "click"
    console.log(eventType)
    item.checked=!item.checked
  //   console.log(item)
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

onChange 事件并不能阻止 click 事件冒泡,它俩不在同一个频道。你需要的是绑定 checkbox 的 onClick 然后阻止冒泡。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...