jQuery关于bind和live具有父子关系的混合使用测试(控制事件的传播)

-----------------------------------题记

最近项目需求:点击出现一个块,再次点击消失,继续点击关闭,并且点击空白处该块消失。那么在这个在这个功能的完成中,因情况需要使用到了live和bind两种事件绑定功能,而使用这两种事件绑定的标签存在父子关系,然后,在测试的过程中控制事件传播的时候出现了些状况,使用event.stopPropagation()子标签事件处还是在父标签事件处是根据该事件传播是冒泡还是捕捉冒泡:如单击子标签,那么触发子标签click事件,再依次触发父标签click事件捕捉:相反。)。所以浪费了大量的事件才调试好,最后还是不明白是什么情况,于是就做出了如下的测试。

-----------------------------------代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<script type="text/javascript" src="jquery-1.8.2.js"></script>
<script type="text/javascript">
    $(function() {
        //使用live添加事件时,不能使用父子关系进行绑定
//        $("#child").live("click", function(event) {
//            alert("child");
//        });
//        $("#child").parents("div").live("click", function(event) {
//            alert("parent");
//        });
        //使用bind添加事件时,可以使用父子关系进行绑定
        $("#parent").bind("click", function(event) {
            alert("parent");
        });
        $("#parent").children().bind("click", function(event) {
//            event.stopPropagation();//冒泡在child处停止事件传播
            alert("child");
        });

        //两bind
        $("#one-p").bind("click", function(event) {
            alert("parent");
        });
        $("#one-c").bind("click", function(event) {
//            event.stopPropagation();//冒泡在child处停止事件传播
            alert("child");
        });

        //两live
        $("#two-p").live("click", function(event) {
            alert("parent");
        });
        $("#two-c").live("click", function(event) {
//            event.stopPropagation();//冒泡在child处停止事件传播
            alert("child");
        });

        //父live子bind
        $("#three-p").live("click", function(event) {
            alert("parent");
        });
        $("#three-c").bind("click", function(event) {
//            event.stopPropagation();//冒泡在child处停止事件传播
            alert("child");
        });

        //父bind子live
        $("#four-p").bind("click", function(event) {
//            event.stopPropagation();//捕获在parent处停止事件传播
            alert("parent");
        });
        $("#four-c").live("click", function(event) {
            alert("child");
        });
    });
</script>
<body>
    <div id="parent">
        <input id="child" type="button" value="click" />
    </div>

    <div id="one-p">
        <input id="one-c" type="button" value="click" />
        <span>parent -> bind & child -> bind ==> 冒泡</span>
    </div>

    <div id="two-p">
        <input id="two-c" type="button" value="click" />
        <span>parent -> live & child -> live ==> 冒泡</span>
    </div>

    <div id="three-p">
        <input id="three-c" type="button" value="click" />
        <span>parent -> live & child -> bind ==> 冒泡</span>
    </div>

    <div id="four-p">
        <input id="four-c" type="button" value="click" />
        <span>parent -> bind & child -> live ==> 捕获</span>
    </div>
</body></html>

-----------------------------------总结

1.使用live绑定事件的时候,不能利用操作父子关系来绑定标签的事件。

2.标签存在父子关系的时候:只有再标签为bind标签为live的时候事件传播为捕获。那么在事件传播为捕获的话只能在父标签绑定的事件停止事件的传播,相反为冒泡的话,则在子标签绑定的事件处停止事件的传播。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 Ajax 发送异步请求时,我们通常需要监听请求的不同阶段,例如请求开始、请求成功、请求失败等,这就需要用到事件绑定jQuery 提供了 bind() 方法来实现事件绑定bind() 方法可以为指定的元素绑定一个或多个事件处理函数,当指定的事件发生时,这些处理函数将会被依次执行。 下面是 bind() 方法的语法: ``` $(selector).bind(event, data, function(eventObject)) ``` 其中,参数 event 表示要绑定事件名称,如 click、mouseover 等;参数 data 表示传递给事件处理函数的额外数据;参数 function(eventObject) 表示事件处理函数,其中 eventObject 表示事件对象。 下面是一个简单的例子,演示如何使用 bind() 方法监听 Ajax 请求的不同阶段: ``` $.ajax({ url: "test.html", beforeSend: function() { $("#loading").show(); }, success: function(result) { $("#content").html(result); }, error: function(jqXHR, textStatus, errorThrown) { alert("请求失败:" + textStatus + " - " + errorThrown); }, complete: function() { $("#loading").hide(); } }); ``` 在上面的例子中,beforeSend、success、error 和 complete 分别对应了 Ajax 请求的不同阶段,我们可以为这些阶段绑定对应的事件处理函数,实现更加精细的控制。 另外,bind() 方法还可以绑定自定义事件,例如: ``` $(document).bind("myEvent", function(event, arg1, arg2) { console.log(arg1 + " - " + arg2); }); $(document).trigger("myEvent", ["Hello", "world"]); ``` 在上面的例子中,我们定义了一个名为 myEvent 的自定义事件,并且绑定了一个事件处理函数,当该事件触发时,该处理函数将会被执行。我们可以通过 trigger() 方法手动触发事件,并传递额外的参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值