js中阻止时间冒泡的方法

来源:互联网 发布:怎么查淘宝账号权重 编辑:程序博客网 时间:2024/05/24 06:09
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  <html>  <head>  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  <title>js阻止事件冒泡的DEMO</title>  <script type="text/javascript">      //阻止冒泡的方法      function stopPP(e)      {          var evt = e || window.event;          //IE用cancelBubble=true来阻止而FF下需要用stopPropagation方法          evt.stopPropagation ? evt.stopPropagation() : (evt.cancelBubble=true);      }  </script>  </head>  <body>  <div style="margin: 150px 400px;width: 700px; height: 550px; background-color: #878788;" onclick="alert('最外层div上的onclick事件');">  <h2>最外层div上的onclick事件</h2>  <div style="margin: 100px; width: 500px; height: 300px; background-color: #545444;" onclick="stopPP(arguments[0]);alert('中间层div上的onclick事件');">  <h3>中间层div上的onclick事件</h3>  <div style="margin: 60px 100px; height: 100px; width: 300px; background-color: red;" onclick="stopPP(arguments[0]);alert('最内层div上的onclick事件');">  <h4>最内层div上的onclick事件”</h4>  </div>  </div>  </div>     </body>  </html>