jQuery prevents bubbling events

The bubbling event is to click on the child node, which will trigger the click event of the parent node and the ancestor node upward.

Here is the html code part:

copy code
<body>
<div id="content">
    outer div element
    <span>Inner span element</span>
    outer div element
</div>

<div id="msg"></div>
</body>
copy code

The corresponding jQuery code is as follows:

copy code
<script type="text/javascript">
$(function(){
    // Bind the click event for the span element
    $('span').bind("click",function(){
        var txt = $('#msg').html() + "<p>The inner span element is clicked.<p/>";//Get html information
        $('#msg').html(txt);// Set html information
    });
    // bind the click event to the div element
    $('#content').bind("click",function(){
        var txt = $('#msg').html() + "<p>The outer div element is clicked.<p/>";
        $('#msg').html(txt);
    });
    // Bind the click event to the body element
    $("body").bind("click",function(){
        var txt = $('#msg').html() + "<p>body element was clicked.<p/>";
        $('#msg').html(txt);
    });
})
</script>
copy code

When the span is clicked, the click event of the div and body will be triggered. When the div is clicked, the body's click event is fired.

How to prevent this bubbling event from happening?

Modify as follows:

copy code
<script type="text/javascript">
$(function(){
       // Bind the click event for the span element
    $('span').bind("click",function(event){
        var txt = $('#msg').html() + "<p>The inner span element is clicked.<p/>";
        $('#msg').html(txt);
        event.stopPropagation(); // stop event from bubbling
    });
    // bind the click event to the div element
    $('#content').bind("click",function(event){
        var txt = $('#msg').html() + "<p>The outer div element is clicked.<p/>";
        $('#msg').html(txt);
        event.stopPropagation(); // stop event from bubbling
    });
    // Bind the click event to the body element
    $("body").bind("click",function(){
        var txt = $('#msg').html() + "<p>body element was clicked.<p/>";
        $('#msg').html(txt);
    });
})
</script>
copy code

event.stopPropagation(); // stop event from bubbling

 

有时候点击提交按钮会有一些默认事件。比如跳转到别的界面。但是如果没有通过验证的话,就不应该跳转。这时候可以通过设置event.preventDefault(); //阻止默认行为 ( 表单提交 )。

下面是案例:

copy code
<script type="text/javascript">
$(function(){
   $("#sub").bind("click",function(event){
         var username = $("#username").val();  //获取元素的值,val() 方法返回或设置被选元素的值。
         if(username==""){     //判断值是否为空
             $("#msg").html("<p>文本框的值不能为空.</p>");  //提示信息
             event.preventDefault();  //阻止默认行为 ( 表单提交 )
         }
   })
})
</script>
copy code

html部分:

copy code
<body>
<form action="test.html">
用户名:<input type="text" id="username" />
<br/>
<input type="submit" value="提交" id="sub"/>
</form>

<div id="msg"></div>
</body>
copy code

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

代码如下:

copy code
<script type="text/javascript">
$(function(){
   $("#sub").bind("click",function(event){
         var username = $("#username").val();  //获取元素的值
         if(username==""){     //判断值是否为空
             $("#msg").html("<p>文本框的值不能为空.</p>");  //提示信息
             return false;
         }
   })
})
</script>
copy code

同理,上面的冒泡事件也可以通过return false来处理。

copy code
<script type="text/javascript">
$(function(){
       // 为span元素绑定click事件
    $('span').bind("click",function(event){
        var txt = $('#msg').html() + "<p>内层span元素被点击.<p/>";
        $('#msg').html(txt);
        return false;
    });
    // 为div元素绑定click事件
    $('#content').bind("click",function(event){
        var txt = $('#msg').html() + "<p>外层div元素被点击.<p/>";
        $('#msg').html(txt);
        return false;
    });
    // 为body元素绑定click事件
    $("body").bind("click",function(){
        var txt = $('#msg').html() + "<p>body元素被点击.<p/>";
        $('#msg').html(txt);
    });
})
</script>
 
 
 
 

JQuery 提供了两种方式来阻止事件冒泡。

方式一:event.stopPropagation();

        $("#div1").mousedown(function(event){
            event.stopPropagation();
        });

方式二:return false;

        $("#div1").mousedown(function(event){
            return false;
        });

但是这两种方式是有区别的。return false 不仅阻止了事件往上冒泡,而且阻止了事件本身。event.stopPropagation() 则只阻止事件往上冒泡,不阻止事件本身。

场景应用: Google 和 百度的联想框,当弹出下拉列表,用户在下拉列表区域按下鼠标时需要让光标仍然保持在文本输入框。

示例测试代码: 当文本输入框获取焦点后,在div1的mousedown事件中采用 event.stopPropagation(); 代码,我们鼠标单击红色区域后文本输入框光标失去。而当我们使用 return false; 代码时,鼠标单击红色区域光标仍然停留在文本输入框内。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="zh-CN" /> 

<title></title>
<script language="JavaScript" type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script>
    $(document).ready(function(){
        $("#div1").mousedown(function(event){
            //event.stopPropagation();
            return false;
        });
        $("#div2").mousedown(function(event){
            alert("trigger mousedown event of rootDiv");
        });
    });
</script>
</head>
<body>
    <div id="rootDiv" style="position:relative;left:400px;top:200px;">
        <div>1.单击输入框,使输入框获取焦点:</div>
        <input id="input1" style="width:250px;" type="text"></input>
        <div id="div2">
            <div id="div1" style="width:200px;height:200px;background-color:red;"><br><br>2.然后再单击这里</div>
        </div>
    </div>
</body>
</html>

 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326521058&siteId=291194637