Home > Web Front-end > JS Tutorial > body text

jquery's on() binding event and off() unbinding event

巴扎黑
Release: 2018-05-15 16:42:37
Original
3729 people have browsed it

off() function is used to remove one or more events bound to an element Event handling function .

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

This function belongs to the jQuery object (instance).

Syntax

jQuery 1.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

ParameterDescription
eventsOptional/ String type one or more event types separated by spaces and optional namespace, such as "click", "focus click", "keydown.myPlugin".
eventsMapObject type is an Object object, each of its properties corresponds to the event type and optional namespace (Parameter events), the attribute value corresponds to the bound event processing function (Parameter handler).
selectorOptional/String type A jQuery selector used to specify which descendant elements can trigger bound events. If this parameter is null or 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).
handlerOptional/Event processing specified by Function type Function.

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

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

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

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

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

返回值

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

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

off()方法的代码示例:

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

html代码

1 <input id="btn1" type="button" value="点击1" />
2 <input id="btn2" type="button" value="点击2" />
3 <a id="a1" href="#">CodePlayer</a>
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事件绑定的事件处理函数btnClick2
 33 // 点击按钮,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和<a>元素)的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对象(实例)。

语法

jQuery 1.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类型指定的事件处理函数

For the optional namespace in the parameter events, please refer to the sample code below.

Regarding the parameter selector, you can simply understand it as: if the parameter is equal to null or is omitted, the event is bound to the current matching element; Otherwise, the event is bound to elements that match the selector selector among the descendant elements of the currently matched element.

this in the parameter handler points to the DOM element that triggers the event among the descendant elements of the current matching element. If the parameter selector is equal to null or is omitted, this points to the current matching element (that is, this element).

on() will also pass in a parameter for handler: the Event object representing the current event.

The return value of parameter handler has the same effect as the return value of the DOM native event processing function. For example, the event handler function of the "submit" (form submission) event returns false, which can prevent the form from being submitted.

If the event handling function handler only returns the false value, you can directly set handler to false.

Return value

on()The return value of the function is of jQuery type, returning the current jQuery object itself.

Important Note: The

on() function does not not bind the element matched by the current jQuery object Instead of specifying an event handler function, bind the event handler function to the elements among their descendant elements that match the selector selector parameters. on()The function does not directly bind events to these descendant elements one by one, but delegates them to the matching elements of the current jQuery object for processing. Due to the DOM level 2 event flow mechanism, when the descendant element selector triggers an event, the event will be passed to all its ancestor elements in event bubbling. When the event flow is passed to the current matching element, jQuery It will determine which descendant element triggered the event. If the element matches the selector selector, jQuery will capture the event and execute the bound event processing function .

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

Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template