JQuery如何阻止事件冒泡

2019-11-25  本文已影响0人  HeloWxl

冒泡事件

就是点击子节点,会向上触发父节点,祖先节点的点击事件。

<style>
    #content {
        width: 140px;
        border: 1px solid blue;
    }
    #msg {
        width: 100px;
        height: 100px;
        margin: 20px;
        border: 1px solid red;
    }
</style>
<body>
      
<div id="content">
        外层div            
    <div id="msg">
         内层div               
    </div>    
</div>
</body>
<script type="text/javascript" src="js/jquery-2.1.4.js"></script>
<script type="text/javascript">
    $(function(){
        // 为内层div绑定click事件
        $("#msg").click(function(){
            alert("我是小div");
        });
        // 为外层div元素绑定click事件
        $("#content").click(function(){
            alert("我是大div");
        });
        // 为body元素绑定click事件
        $("body").click(function(){
            alert("我是body");
        });
    });
</script>
</html>
小div.png
大div.png
body.png

使用阻止冒泡 event.stopPropagation();

<script type="text/javascript">
    $(function(){
        // 为内层div绑定click事件
        $("#msg").click(function(event){
            alert("我是小div");
            event.stopPropagation();    //  阻止事件冒泡
        });
        // 为外层div元素绑定click事件
        $("#content").click(function(event){
            alert("我是大div");
            event.stopPropagation();    //  阻止事件冒泡
        });
        // 为body元素绑定click事件
        $("body").click(function(event){
            alert("我是body");
            event.stopPropagation();    //  阻止事件冒泡
        });
    });
</script>
阻止事件冒泡.png

还有一种防止默认行为的方法就是return false。效果一样。

    <script type="text/javascript">
        $(function(){
            $("#sub").click(function(event){
                //获取元素的值,val() 方法返回或设置被选元素的值。
                var username = $("#username").val();  
                //判断值是否为空
                if(username==""){ 
                    //提示信息
                    //alert("文本框的值不能为空");
                    $("#msg").html("<p>文本框的值不能为空.</p>"); 
                  //阻止默认行为 ( 表单提交 )
                    //event.preventDefault();  
                  return false;
                }
            });
        });
    </script>
    <script type="text/javascript">
    $(function(){
        // 为内层div绑定click事件
        $("#msg").click(function(event){
            alert("我是小div");
            //event.stopPropagation();    //  阻止事件冒泡
                return false;
        });
     // 为外层div元素绑定click事件
        $("#content").click(function(event){
            alert("我是大div");
            //event.stopPropagation();    //  阻止事件冒泡
                 return false;
        });
        // 为body元素绑定click事件
        $("body").click(function(event){
            alert("我是body");
            //event.stopPropagation();    //  阻止事件冒泡
                return false;
        });
    });
false阻止事件冒泡.png

提交按钮阻止事件

<body>
<form action="test.html">
    用户名:<input type="text" id="username" />
    <br/>
    <input type="submit" value="提交" id="sub"/>
    <div id="msg"></div>
</form>
</body>
<script type="text/javascript" src="js/jquery-2.1.4.js"></script>
<script type="text/javascript">
    $(function(){
        $("#sub").click(function(event){
            //获取元素的值,val() 方法返回或设置被选元素的值。
            var username = $("#username").val();
            //判断值是否为空
            if(username==""){
                //提示信息
                //alert("文本框的值不能为空");
                $("#msg").html("<p>文本框的值不能为空.</p>");
                //阻止默认行为 ( 表单提交 )
                event.preventDefault();
            }
        });
    });
</script>
image.png
上一篇 下一篇

猜你喜欢

热点阅读