js原生子級元素阻止父級元素冒泡事件


<html>
 <head>
    <style type="text/css">
         #hide{
          width:75%;height:80px;background:skyblue;display:block;
         }
         .hander{cursor:pointer;}
         input{
          margin:5 0 0 900;
         }
    </style>
       <script>
        //不用window.onload也可以
      document.documentElement.onclick = function(){
       document.getElementById('hide').style.display = 'none';  
      }
     //阻止冒泡事件方法
     function stopPropagation(e) {  
      e = e || window.event;  
      if(e.stopPropagation) { //W3C阻止冒泡方法  
          e.stopPropagation();  
      } else {  
          e.cancelBubble = true; //IE阻止冒泡方法  
      }  
  }
  //方法必須要放在window.onload下
  window.onload = function(){
   document.getElementById("hide").onclick = function(e){
        stopPropagation(e);
      }
      document.getElementById('btn_show').onclick = function(e) {  
          document.getElementById('hide').style.display = 'block';  
          stopPropagation(e);  
   }
  }
  
      </script>
 </head>
 <body>
  <div id="hide" class="hander">click here nothing happen,you can click beside this area</div>
  <input type="button" id="btn_show" value="show" class="hander"/>
 </body>
</html>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM