<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <style> #a{ 100px; height: 100px; background: red; border: 1px solid; } #box{ 200px; height: 200px; background: blue; border: 1px solid; } </style> </head> <body> <div id="box"> <div id='a'></div> </div> </body> </html> <script> document.getElementById("box").onclick=function(){ console.log("box"); } document.getElementById("a").onclick=function(even){ even.stopPropagation(); //将even(事件)作为参数,就是这个“点击”事件,通过这个even,可以获取到event.target.stopPropagation()解决事件冒泡
console.log("a"); }
</script>