[英]How do I prevent a parent's onclick event from firing when a child anchor is clicked?
[英]how do I prevent parent onclick action on child using javascript
您好,我在 onclick function 中應用 onclick function,但是我在父級上執行的 function 也在子級上應用,如何防止子級上的 function。 在我的代碼中,我通過點擊紅色框在 google.com 上重定向,但是當我點擊藍色框時,google.com 上也出現了如何阻止此選項
我的代碼
function parent(){ window.location.href='https://www.google.com/'; } function child(){ alert('Hello this is the blue button'); }
<div style="background: red; padding: 50px; height:200px; width:500px; " onclick="parent()"> <div style="background: blue; padding: 5px; color:white;" onclick="child()">Click me</div> </div>
希望這會奏效。
function child(){
event.stopPropagation();
alert('Hello this is the blue button');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.