jQuery 中可以使用 off 方法解除事件绑定. off() 方法通常用于移除通过 on() 方法添加的事件处理程序.
自 jQuery 版本 1.7 起, off() 方法是 unbind(),die() 和 undelegate() 方法的新的替代品. 该方法给 API 带来很多便利, 我们推荐使用该方法, 它简化了 jQuery 代码库.
off 方法:
不带参数, 解绑元素上所有的方法
带一个参数, 解绑这类型的方法
带二个参数, 解绑这类型指定的方法
示例:
- <html>
- <head>
- <title></title>
- </head>
- <style>
- </style>
- <script src="http://code.jquery.com/jquery-latest.js"></script>
- <script>
- $(function(){
- function test(){
- alert("wjw")
- }
- function test1(){
- alert("wjw")
- }
- $('button').click('test')
- $('button').click('test1')
- $('button').mouseout(function(){
- alert("wjw")
- })
- //$('button').off(); // 解除 button 上的所有绑定事件
- //$('button').off('click') // 解除 button 上的所有 click 事件
- $('button').off('click',test)
- })
- </script>
- <body>
- <button > 按钮 </button>
- </body>
- </HTML>
更多相关知识请关注前端学习网站 https://www.html.cn/
来源: http://www.css88.com/qa/jquery/19097.html