• 冒泡


    #box1{
        width: 200px;height: 200px;border: 1px solid #333;
    }
    #box2{
        width: 100px;height: 100px;margin: 50px;border: 1px solid #333;
    }
    #span{
         width: 50px;height: 50px;border: 1px solid #333;margin: 25px;display: block;
    }
    <div id="box1" class="box1">
         <div id="box2" class="box2">
              <span id="span">点击</span>
         </div>
    </div>
    $("#span").click(function(){
         alert("我是span")
         if(event.stopPropagation){
              event.stopPropagation();
         }else{
              event.cancelBubble = true;
         }
     })
     $("#box2").click(function(){
          alert("我是box2")
          if(event.stopPropagation){
               event.stopPropagation();
          }else{
               event.cancelBubble = true;
          }
     })
     $("#box1").click(function(){
          alert("我是box1")
          if(event.stopPropagation){
               event.stopPropagation();
          }else{
               event.cancelBubble = true;
           }
    })

    冒泡解决

     <script>
            $("#span").click(function(){
                alert("我是span")
                if(event.stopPropagation){
                    event.stopPropagation();
                }else{
                    event.cancelBubble = true;
                }
            })
            $("#box2").click(function(){
                
                alert("我是box2")
    
                if(event.stopPropagation){
                    event.stopPropagation();
                }else{
                    event.cancelBubble = true;
                }
            })
            $("#box1").click(function(){
                alert("我是box1")
                
                if(event.stopPropagation){
                    event.stopPropagation();
                }else{
                    event.cancelBubble = true;
                }
            })
        </script>
        
       
    </body>
    </html>
  • 相关阅读:
    判断是否声明了某个特性
    如何月份英文缩写字符串转日期
    要捕捉System.Exception异常吗
    jenkins发布到nuget
    C# – Record, Class, Struct
    Google Maps Embed API & JavaScript API
    图片综合知识
    ASP.NET Core – ADO.NET
    CSS – 管理
    JavaScript Library – PhotoSwipe
  • 原文地址:https://www.cnblogs.com/namehou/p/10150287.html
Copyright © 2020-2023  润新知