jquery里面的绑定与解除绑定事件

// PS:现在都用on来绑定事件,用off来解除绑定,on代替了下列所有方法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        div,
        .div1{
            width: 300px;
            height: 300px;
            background-color: yellow;
        }
    </style>
</head>
<body>
<div id="divID"></div>
<p>Hit Me!</p>
<div class="div1"></div>
</body>
<script src="./../js/jquery-1.12.4.min.js"></script>
<script>
    function f1() {
        alert("hello, bind绑定事件");
    }
    // 通过bind绑定事件,而unbind解除bind绑定的事件,要与之对应
    // bind还支持链式调用
    $("#divID").bind("mouseenter", f1).css("backgroundColor", "green");
    $("#divID").unbind("mouseenter");
  
  	// PS:在JQ1.7版本之后已移除
    // 为DOM不存在的元素/动态加载的元素,绑定事件可以用live方法,而die解除live绑定的事件,要与之对应
    // $("body").append("<a id='anchor'>I go no where</a>");
    // function f2() {
    //     alert("hello, live绑定事件");
    // }
    // $("#anchor").live("click", f2);
    // $("#anchor").die("click");


    // delegate方法为指定的元素(属于被选元素的子元素)添加一个或多个事件处理程序,并规定当这些事件发生时运行的函数。
    // 使用delegate方法的事件处理程序适用于当前或未来的元素(比如由脚本创建的新元素)。
    // delegate还支持链式调用
    // undelegate是移除delegate的绑定事件
    function f3() {
        alert("hello, delegate绑定事件");
    }
    $("body").delegate("p", "click", f3).css("color", "green");
    $("body").undelegate("p", "click");
</script>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值