jQuery 中的 unbind()

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

语法结构:

[javascript] view plaincopy
  1. unbind([type][, data]);  


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

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

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

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

请看下面的举例:

[javascript] view plaincopy
  1. <script src="http://www.gamejzy.com/js/jquery.js" type="text/javascript"></script>  
  2. <style>  
  3. .info {  
  4.     background:#ffff66;  
  5. }  
  6. </style>  
  7. <input type="button" id="btn" value="点击我" /> <input type="button" id="delAll" value="删除全部绑定函数" /> <input type="button" id="delFun2" value="删除第二个绑定函数" /><br />  
  8. <div class="info"></div>  
  9. <script type="text/javascript">  
  10. $(document).ready(function(){  
  11.     // 为id为btn的按钮添加绑定事件  
  12.     $("#btn").bind('click', fun1=function(){  
  13.         $(".info").append('<p>绑定函数1</p>');  
  14.     }).bind('click', fun2=function(){  
  15.         $(".info").append('<p>绑定函数2</p>');  
  16.     }).bind('click', fun3=function(){  
  17.         $(".info").append('<p>绑定函数3</p>');  
  18.     })  
  19.     $("#delAll").bind('click', function(){  
  20.         $("#btn").unbind(); //删除全部绑定事件  
  21.     })  
  22.     $("#delFun2").bind('click', function(){  
  23.         $("#btn").unbind('click', fun2);  //删除第二个绑定函数  
  24.     })  
  25. })  
  26. </script>  


效果展示图:

posted on 2014-09-18 17:19  水寒03  阅读(193)  评论(0编辑  收藏  举报

导航