jQuery unbind() 方法

jQuery 中的 unbind() 方法是 bind() 方法的反向操作,从每一个匹配的元素中删除绑定的事件。

语法结构:

unbind([type][, data]);  

type是事件类型,data为将要移除的事件。具体说明如下:

 

1、如果没有参数,则删除所有的绑定事件;

2、如果提供了事件类型(type)作为参数,则只删除该类型的绑定事件;

3、如果把在绑定时传递的处理函数作为第2个参数,则只有这个特定的事件处理函数被删除。

请看下面的举例:

<script src="http://www.gamejzy.com/js/jquery.js" type="text/javascript"></script>
<style>
.info {
    background:#ffff66;
}
</style>
<input type="button" id="btn" value="点击我" /> <input type="button" id="delAll" value="删除全部绑定函数" /> <input type="button" id="delFun2" value="删除第二个绑定函数" /><br />
<div class="info"></div>
<script type="text/javascript">
$(document).ready(function(){
    // 为id为btn的按钮添加绑定事件
    $("#btn").bind('click', fun1=function(){
        $(".info").append('<p>绑定函数1</p>');
    }).bind('click', fun2=function(){
        $(".info").append('<p>绑定函数2</p>');
    }).bind('click', fun3=function(){
        $(".info").append('<p>绑定函数3</p>');
    })
    $("#delAll").bind('click', function(){
        $("#btn").unbind(); //删除全部绑定事件
    })
    $("#delFun2").bind('click', function(){
        $("#btn").unbind('click', fun2);  //删除第二个绑定函数
    })
})
</script>

效果展示图:

 

posted @ 2016-02-29 16:15  MaoSan-Jerry  阅读(1758)  评论(0编辑  收藏  举报