为什么stopPropagation无法阻止页面导航?

问题描述 投票:1回答:1

这里是演示:https://codesandbox.io/s/rough-sun-znrd2

我在a的子元素button处调用了stopPropagation,并且a也没有收到click事件。但是页面仍然可以导航。

javascript html browser dom-events
1个回答
0
投票

代替stopPropagation防止事件冒泡,您需要使用event.preventDefault取消默认行为

 const onClickButton = (event: MouseEvent) => {
      console.log("click Button");
      event.preventDefault();
      aElm.click();
    };
© www.soinside.com 2019 - 2024. All rights reserved.