stopPropagation()
举例:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>js_excise</title> </head> <body> <div id="box1" style="background-color: #00FFFF;width: 70px;height: 70px;"> </div> <input type="text" id="box2"> <script type="text/javascript"> document.body.onclick=function(){ this.style.backgroundColor=‘yellow‘ } document.getElementById(‘box1‘).onclick= function(e){ var ev = e || window.event //阻止冒泡 ev.stopPropagation() this.style.backgroundColor = ‘pink‘ } document.getElementById(‘box2‘).onclick = function(e){ var ev = e || window.event //阻止冒泡 ev.stopPropagation() } </script> </body> </html>
输出:body元素背景不会变成黄色
原文:https://www.cnblogs.com/abner-pan/p/12828631.html