jQuery学习笔记之.unbind()

本文目标

1 了解.unbind()的几种调用方式及其参数含义
2 了解通过函数名解绑事件
3 了解利用命名空间解绑事件

学习资料

官方描述文档: http://api.jquery.com/unbind/#unbind-eventType-handler/
中文描述文档: http://www.css88.com/jqapi-1.9/unbind/
建议: 如果时间充裕还是看官方文档

官方描述

Remove a previously-attached event handler from the elements.

移除通过.bind()附加到元素上的事件处理过程
官方在1.7+,推荐使用.on().off()为元素附加、移除事件绑定

四种使用方式

1 .unbind( eventType [, handler ] ) version added: 1.0
2 .unbind( eventType, false ) version added: 1.4.3
3 .unbind( event ) version added: 1.0
4 .unbind() version added: 1.0

eventType
Type: String
A string containing a JavaScript event type, such as click or submit

参数:eventType
参数类型:String
参数说明:一个包含JavaScript事件类型的字符串,例如”click” or “submit”

handler
Type: Function( Event eventObject )
The function that is to be no longer executed.

参数:handler
参数类型:Function
参数说明:不再被执行的函数

false
Type: Boolean
Unbinds the corresponding ‘return false’ function that was bound using .bind( eventType, false ).

参数:false
参数类型:Boolean
参数说明:解绑一个被利用.bind(eventType,false)这种方式绑定其效果等效于”return false”的函数

event
Type: Event
A jQuery.Event object

参数:event
参数类型:Event
参数说明:jQuery.Event对象

函数说明

    $( "#foo" ).unbind();

这种写法将移除$("#foo")元素上的全部利用.bind()绑定的事件.我们可以使用事件类型这个参数是解绑事件的范围更加精确,例如:

    $( "#foo").unbind( "click" );

By specifying the click event type, only handlers for that event type will be unbound. This approach can still have negative ramifications if other scripts might be attaching behaviors to the same element, however. Robust and extensible applications typically demand the two-argument

通过指定事件类型,仅这事件类型对应的处理过程将被解除绑定;但这种将带来一些非预期的效果,例如,在程序的其它地方为同一元素的相同事件类型绑定了其它的事件处理过程,这种方式将导致这些处理过程也将被从这个元素上移除.为了避免这个问题,我们可以再增加一个参数handler

function sayHi(){alert("Hi");}$("#itemNav").find('#a2').bind("click",sayHi);$("#itemNav").find('#a2').unbind('click', sayHi);

By naming the handler, we can be assured that no other functions are accidentally removed.

通过事件处理过程的名词,我们能够确保其它的处理过程不会被意外的移除.
但下面这种方式将无法达到正常效果:

$("#itemNav").find('#a2').bind("click",function sayHi(){alert("Hi");});$("#itemNav").find('#a2').unbind('click', function sayHi(){alert("Hi");});

Even though the two functions are identical in content, they are created separately and so JavaScript is free to keep them as distinct function objects. To unbind a particular handler, we need a reference to that function and not a different one that happens to do the same thing

虽然这两个函数在内容上是相同的,但他们是被单独创建的,JavaScript认为它们是不同的函数对象.对于解绑一个特殊的事件处理过程,我们需要的是这个函数的引用而不是做相同事情的不同函数.

Instead of maintaining references to handlers in order to unbind them, we can namespace the events and use this capability to narrow the scope of our unbinding actions. As shown in the discussion for the .bind() method, namespaces are defined by using a period (.) character when binding a handler:

在解绑事件处理过程时我们可以用事件命名空间来代替对事件处理过程的引用,利用这一特性可以缩小我们解绑事件这一动作的范围.命名空间是利用一个(.)符号在绑定事件处理过程的时候定义的.

$( "#foo" ).bind( "click.myEvents", handler );

When a handler is bound in this fashion, we can still unbind it the normal way

当一个事件处理过程是利用这种方式绑定到元素上时,我们仍然能利用正常的方式去解绑它.

$( "#foo" ).unbind( "click" );

However, if we want to avoid affecting other handlers, we can be more specific

如果我们想避免影响到其它的事件处理过程,我们能够用更具体方式:

$( "#foo" ).unbind( "click.myEvents" );

我们也能够在不考虑事件类型的情况下解绑同一命名空间下的全部事件处理过程:

$( "#foo" ).unbind( ".myEvents" );

It is particularly useful to attach namespaces to event bindings when we are developing plug-ins or otherwise writing code that may interact with other event-handling code in the future.

当我们开发插件或写一些在未来可能与其它事件处理过程相互影响的代码时,对于使用命名空间进行事件绑定是非常有用到.

The third form of the .unbind() method is used when we wish to unbind a handler from within itself

.unbind()方法的第三种形式.unbind(event)是用于当我们希望在一个事件处理过程内部去解绑这个事件处理过程时.

var timesClicked = 0;$( "#foo" ).bind( "click", function( event ) {alert( "The quick brown fox jumps over the lazy dog." );timesClicked++;if ( timesClicked >= 3 ) {$( this ).unbind( event );}});

The handler in this case must take a parameter, so that we can capture the event object and use it to unbind the handler after the third click. The event object contains the context necessary for .unbind() to know which handler to remove.

在这种情况下,这事件处理过程能够必须提供一个参数,以便我们能够获得事件对象并利用在第三次点击之后去解绑这个事件处理过程.这事件对象包含了.unbind()方法了解哪个事件处理过程要被移除所需的上下问环境.

相关Demo

example1

function sayHi(){alert("Hi");}$("#itemNav").find('#a2').bind("mouseenter mouseleave",sayHi);$("#itemNav").find('#a2').unbind('mouseenter mouseleave',sayHi);

*不解

Note: Using a proxied function to unbind an event on an element will unbind all proxied functions on that element, as the same proxy function is used for all proxied events. To allow unbinding a specific event, use unique class names on the event (e.g. click.proxy1, click.proxy2) when attaching them.

官网中有这样一段话没看明白,若有理解的还望分享一下!

jQuery学习笔记之unbind()相关推荐

  1. jQuery学习笔记之DOM操作、事件绑定(2)

    jQuery学习笔记之DOM操作.事件绑定(2) --------------------学习目录------------------------ 4.DOM操作 5.事件绑定 源码地址: https ...

  2. jQuery学习笔记(二)

    jQuery学习笔记(二) 二.管理jQuery包装集 1.创建新的元素 使用HTML DOM 创建元素 var select=document.createElement("select& ...

  3. javaweb(03) jQuery学习笔记

    javaweb(03) jQuery学习笔记 jQuery介绍 什么是jQuery jQuery,顾名思义,也就是 JavaScript 和查询(Query),它就是辅助 JavaScript 开发的 ...

  4. jQuery学习笔记--目录

    jQuery学习笔记--Helloworld jQuery学习笔记--DOM对象和jQuery对象 jQuery学习笔记--jQuery选择器 jQuery学习笔记--jQuery的DOM操作 jQu ...

  5. 【转载】jQuery学习笔记

    jQuery学习笔记 1 基础 HTML :一个人的裸体,是一个人的物质基础,是一个结构. CSS :一个人的漂亮外衣,使一个人看起来不是那么原始,修饰了一个人. JavaScript :一个人的灵魂 ...

  6. jQuery学习笔记:Ajax(二)

    接上篇"jQuery学习笔记:Ajax(一)". 3.jQuery.get(url,[data],[callback],[type])通过远程 HTTP GET 请求载入信息. 这 ...

  7. jQuery学习笔记02:核心部分

    jQuery学习笔记:核心部分 一.$(expr) 1.说明 $(expr) 该函数通过CSS选择器.XPath或html代码来匹配目标元素 参数:expr(字符串,一个查询表达式或一段html字符串 ...

  8. jQuery学习笔记01:初试jQuery

    jQuery学习笔记01:初试jQuery 一.下载jQuery jQuery官网:https://jquery.com 二.案例演示--Welcome to jQuery World 1.在WebS ...

  9. jquery学习笔记(-)

    Jquery学习笔记(-) Jquery的简介 Jquery是继prototype之后又一个优秀的Javascrīpt框架.它是轻量级的js库(压缩后只有21k),兼容各种浏览器(IE6.0+,FF1 ...

最新文章

  1. Python 自动化 - 浏览器chrome打开F12开发者工具自动Paused in debugger调试导致无法查看网站资源问题原因及解决方法,javascript反调试问题处理实例演示
  2. linux刷除U盘grub,删除linux系统出现grub rescue
  3. java 分布式事务_Java核心知识 Spring原理十五 JPA 原理
  4. ui界面表单设计素材模板,实用可临摹
  5. 如何测试一个数组是否包含指定的值
  6. 基于node.js的微博博客实现
  7. Python爬虫入门学习--中国大学排名
  8. 钩子(hook)编程
  9. Adobe Creative Cloud 2022 (macOS、Windows) TNT 合集
  10. Mac App自动化测试
  11. 车载android 电源管理
  12. JPEG压缩编码算法原理
  13. 手机邮箱怎么弄_手机邮箱设置
  14. 【单片机】【数码管】数码管显示
  15. cad角度怎么画_软件CAD | 直线amp;构造线
  16. 自学鸿蒙应用开发(7)- Picker组件
  17. 微信退还钱什么时候到账?
  18. 春节微信刷屏的拜年兔,幕后是卑职献祭掉的头发
  19. 如何彻底解决浏览器导航被劫持为www.hao123.com
  20. javafx 制作五子棋游戏——简单MVC框架

热门文章

  1. 5G切片 NFV SDN 架构笔记
  2. 移动端App开发的三种开发技术对比
  3. JAVA毕业设计HTML5运河古城网站计算机源码+lw文档+系统+调试部署+数据库
  4. Theseus和Ariadne
  5. git Please tell me who you are...解决方法
  6. 移动端H5·html转图片之html2canvas
  7. 江西移动集团通讯录 v4.68
  8. vscode 配置代码片段
  9. Vscode 快捷键生成代码片段
  10. 第22章 OTP介绍