點選裡面div或外面div的事件冒泡問題
阿新 • • 發佈:2019-02-02
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <div style="width: 400px;height: 400px;background-color: #678678;font:3em bold;" id="outDiv"> out <div style="width: 200px;height: 200px;margin: 50px;background-color: #eaceac;font:1em bold;" id="inDiv">in</div> </div> <script> document.getElementById("inDiv").addEventListener('click',function(e){ alert('in'); e.stopPropagation();//終止事件冒泡 },false); document.getElementById("outDiv").addEventListener('click',function(e){ alert('out'); },false); </script> </body> </html>