子元素 点击事件 被父元素覆盖

1,253 阅读1分钟

重点

  • e.preventDefault() 阻止事件冒泡
  • e.stopPropagation() 阻止a元素的默认跳转行为。
demo
<div onClick={
    ()=>{this.parent(params);
}>
    <div onClick={(e)=>{
          this.son(params);
          e.stopPropagation();
        }
    }>
        <a href="#" onClick={(e)=>{
              this.sonLink();
              e.stopPropagation();
              e.preventDefault();
            }
          }>点击子元素的a链接</a>
    </div>
</div>
  1. 子元素添加
e.stopPropagation();
  1. 子元素内a链接添加
e.stopPropagation();
e.preventDefault();