jquery's on() binding event and off() unbinding event - no trust

巴扎黑
Release: 2017-06-30 13:53:50
Original
1180 people have browsed it

off()function is used toremove one or more events bound to an elementEvent handlingfunction.

off()The function is mainly used to unblock the event processing function bound by the on() function.

This function belongs to thejQueryobject (instance).

Syntax

jQuery1.7 Add this function. It mainly has the following two forms of usage:

Usage one:

jQueryObject.off( [ events [, selector ] [, handler ] ] )
Copy after login

Usage two:

jQueryObject.off( eventsMap [, selector ] )
Copy after login

Parameters

Parameter Description
events Optional/ String type one or more event types separated by spaces and optionalnamespace, such as "click", "focus click", "keydown.myPlugin".
eventsMap Object type is anObject object, each of its properties corresponds to the event type and optional namespace (Parameterevents), the attribute value corresponds to the bound event processing function (Parameterhandler).
selector Optional/String type A jQuery selector used to specify which descendant elements can trigger bound events. If this parameter isnullor is omitted, it means that the current element itself is bound to the event (the actual triggerer may also be a descendant element, as long as the event stream can reach the current element).
handler Optional/Event processing specified by Function typeFunction.

off()函数将会移除当前匹配元素上为后代元素selector绑定的events事件的事件处理函数handler

如果省略参数selector,则移除为任何元素绑定的事件处理函数。

参数selector必须与通过on()函数添加绑定时传入的选择器一致。

如果省略参数handler,则移除指定元素指定事件类型上绑定的所有事件处理函数。

如果省略了所有参数,则表示移除当前元素上为任何元素绑定的任何事件类型的任何事件处理函数。

返回值

off()函数的返回值jQuery类型,返回当前jQuery对象本身。

实际上,off()函数的参数全是筛选条件,只有匹配所有参数条件的事件处理函数才会被移除。参数越多,限定条件就越多,被移除的范围就越小。

off()方法的代码示例:

容易忽略的点:off所解除元素的绑定事件,其中选择器必须和on绑定事件时所用的选择器一致。

html代码


1 2 3 CodePlayer
Copy after login

View Code

页面加载时执行的jquery代码


1 function btnClick1(){ 2 alert( this.value + "-1" ); 3 } 4 5 function btnClick2(){ 6 alert( this.value + "-2" ); 7 } 8 9 var $body = $("body");10 11 // 给按钮1绑定点击12 $body.on("click", "#btn1", btnClick1 );13 14 // 给按钮2绑定点击15 $body.on("click", "#btn2", btnClick2 );16 17 //为所有a元素绑定click、mouseover、mouseleave事件18 $body.on("click mouseover mouseleave", "a", function(event){19 if( event.type == "click" ){20 $body.off("click", "#btn1");//取消btn1的绑定事件。成功执行21 alert("点击事件");22 alert("ddd");23 }else if( event.type == "mouseover" ){24 $(this).css("color", "red");25 }else{26 $(this).css("color", "blue");27 28 }29 });30 31 32 // 移除body元素为所有button元素的click事件绑定的事件处理函数btnClick233 // 点击按钮,btnClick1照样执行34 $body.off("click", ":button", btnClick2);35 36 37 // 点击按钮1,不会执行任何事件处理函数38 // $body.off("click", "#btn1");39 40 41 // 注意: $body.off("click", ":button"); 无法移除btn1的点击事件,off()函数指定的选择器必须与on()函数传入的选择器一致。42 43 44 // 移除body元素为所有元素(包括button和元素)的click事件绑定的所有处理函数45 // 点击按钮或链接,都不会触发执行任何事件处理函数46 // $("body").off("click");47 48 49 // 移除body元素为所有元素的任何事件绑定的所有处理函数50 // 点击按钮,或点击链接或者鼠标移入/移出链接,都不会触发执行任何事件处理函数51 // $("body").off( );
Copy after login

View Code

on()函数用于为指定元素的一个或多个事件绑定事件处理函数

此外,你还可以额外传递给事件处理函数一些所需的数据。

从jQuery 1.7开始,on()函数提供了绑定事件处理程序所需的所有功能,用于统一取代以前的bind()、 delegate()、 live()等事件函数。

即使是执行on()函数之后新添加的元素,只要它符合条件,绑定的事件处理函数也对其有效。

此外,该函数可以为同一元素、同一事件类型绑定多个事件处理函数。触发事件时,jQuery会按照绑定的先后顺序依次执行绑定的事件处理函数。

要删除通过on()绑定的事件,请使用off()函数。如果要附加一个事件,只执行一次,然后删除自己,请使用one()函数。

该函数属于jQuery对象(实例)。

语法

jQuery1.7 新增该函数。其主要有以下两种形式的用法:

用法一

jQueryObject.on( events [, selector ] [, data ], handler )
Copy after login

用法二

jQueryObject.on( eventsMap [, selector ] [, data ] )
Copy after login

参数

参数 描述
events String类型一个或多个用空格分隔的事件类型和可选的命名空间,例如"click"、"focus click"、"keydown.myPlugin"。
eventsMap Object类型一个Object对象,其每个属性对应事件类型和可选的命名空间(参数events),属性值对应绑定的事件处理函数(参数handler)。
selector 可选/String类型一个jQuery选择器,用于指定哪些后代元素可以触发绑定的事件。如果该参数为null或被省略,则表示当前元素自身绑定事件(实际触发者也可能是后代元素,只要事件流能到达当前元素即可)。
data 可选/任意类型触发事件时,需要通过event.data传递给事件处理函数的任意数据。
handler Function类型指定的事件处理函数

关于参数events中可选的命名空间,请参考最下面的示例代码。

关于参数selector,你可以简单地理解为:如果该参数等于null或被省略,则为当前匹配元素绑定事件;否则就是为当前匹配元素的后代元素中符合selector选择器的元素绑定事件。

参数handler中的this指向当前匹配元素的后代元素中触发该事件的DOM元素。如果参数selector等于null或被省略,则this指向当前匹配元素(也就是该元素)。

on()还会为handler传入一个参数:表示当前事件的Event对象。

参数handler的返回值与DOM原生事件的处理函数返回值作用一致。例如"submit"(表单提交)事件的事件处理函数返回false,可以阻止表单的提交。

如果事件处理函数handler仅仅只为返回false值,可以直接将handler设为false

返回值

on()函数的返回值为jQuery类型,返回当前jQuery对象本身。

重要说明

on()函数并不是为当前jQuery对象匹配的元素绑定事件处理函数,而是为它们的后代元素中符合选择器selector参数的元素绑定事件处理函数。on()函数并不是直接为这些后代元素挨个绑定事件,而是委托给当前jQuery对象的匹配元素来处理。由于DOM 2级的事件流机制,当后代元素selector触发事件时,该事件会在事件冒泡中传递给其所有的祖辈元素,当事件流传递到当前匹配元素时,jQuery会判断是哪个后代元素触发了事件,如果该元素符合选择器selector,jQuery就会捕获该事件,从而执行绑定的事件处理函数

The above is the detailed content of jquery's on() binding event and off() unbinding event - no trust. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!