How to ban an event in jquery

WBOY
Release: 2022-05-23 10:17:05
Original
2717 people have browsed it

Jquery method to prohibit an event: 1. Use the unbind() method, the syntax is "element object.unbind (forbidden event)"; 2. Use the off() method, the syntax is "element object. off(forbidden event or namespace)", this method is usually used to remove event handlers added through the on() method.

How to ban an event in jquery

The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.

How to ban an event in jquery

1. unbind() method

The unbind() method removes the event handler of the selected element.

This method can remove all or selected event handlers, or terminate the execution of the specified function when an event occurs.

This method can also unbind the event handler through the event object. This method is also used to unbind events within itself (such as deleting the event handler after the event has been triggered a certain number of times).

Note: If no parameters are specified, the unbind() method will remove all event handlers for the specified element.

Note: The unbind() method applies to any event handler added by jQuery.

Syntax

$(selector).unbind(event,function,eventObj)
Copy after login
  • event Optional. Specifies one or more events to be removed from the element. Multiple event values ​​separated by spaces. If only this parameter is specified, all functions bound to the specified event are removed.

  • function Optional. Specifies the name of the function to unbind the specified event from the element.

  • eventObj Optional. Specifies the removed event object to use. The eventObj parameter comes from the event binding function.

The example is as follows:




 
123 



这是一个段落。

这是另外一个段落。

点击任意段落(p 元素),该段落就会消失。

Copy after login

Output result:

How to ban an event in jquery

2. off() method

off() method is usually used to remove event handlers added through on() method.

Syntax

$(selector).off(event,selector,function(eventObj),map)
Copy after login

event Required. Specifies one or more events or namespaces to be removed from the selected elements. Multiple event values ​​separated by spaces. Must be a valid event.

selector Optional. Specifies the selector initially passed to the on() method when adding an event handler.

function(eventObj) Optional. Specifies a function to run when an event occurs.

map specifies an event map ({event:function, event:function, ...}) containing one or more events to be added to the element, and a function to run when the event occurs.

Examples are as follows:




 
123 



点击这个段落修改它的背景颜色。

点击一下按钮再点击这个段落( click 事件被移除 )。

Copy after login

Output results:

How to ban an event in jquery

## Recommended related video tutorials:

jQuery video tutorial

The above is the detailed content of How to ban an event in jquery. 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 [email protected]
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!