简体   繁体   中英

how do I prevent parent onclick action on child using javascript

Hello am applying onclick function inside onclick function, but the function which I am performing on parent is also applying on child div, how to to prevent that function on child. In my code I redirecting on google.com by click on red box but when I click on blue box is alos going on google.com how to prevent this option

my code

 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>

Hope this will work.

function child(){
  event.stopPropagation();
  alert('Hello this is the blue button');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM