jQuery-移除事件

unbind()--unbind(type,data)

第一个参数事件类型,第二个参数将要移除的函数

(1)、如果没有参数,则删除所有绑定的函数

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

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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script type="text/javascript" src='jquery-3.2.1.min.js'></script>
    <script type="text/javascript">
        $(function(){
            $('#btn').bind('click',fn1=function(){
                $('#test').append('<p>我的绑定函数1</p>');
            }).bind('click',fn2=function(){
                $('#test').append('<p>我的绑定函数2</p>');
            }).bind('click',fn3=function(){
                $('#test').append('<p>我的绑定函数3</p>');
            });
            $('#delAll').click(function() {
                $('#btn').unbind('click',fn2);
            });
        });
    </script>
</head>
<body>
    <button id="btn">单击我</button>
    <button id="delAll">删除所有事件</button>
    <div id="test"></div>
</body>
</html>

one()--该方法可以为元素绑定处理函数,当处理函数触发一次后,立即被删除

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script type="text/javascript" src='jquery-3.2.1.min.js'></script>
    <script type="text/javascript">
        $(function(){
            $('#btn').one('click',function(){
                $('#test').append('<p>我的绑定函数1</p>');
            }).one('click',function(){
                $('#test').append('<p>我的绑定函数2</p>');
            }).one('click',function(){
                $('#test').append('<p>我的绑定函数3</p>');
            });
        });
    </script>
</head>
<body>
    <button id="btn">单击我</button>
    <button id="delAll">删除所有事件</button>
    <div id="test"></div>
</body>
</html>

 

posted @ 2017-05-18 19:48  影子疯  阅读(15661)  评论(0编辑  收藏  举报