Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use off in jquery

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will share with you the relevant knowledge points about how to use off in jquery. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

In jquery, the off () method is used to remove event handlers for one or more events bound on an element; it is usually used to remove event handlers added through on (), with the syntax "element object .off (event or namespace removed from the selected element, selector, function that runs when the event occurs, map)".

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

What is the use of off in jquery

The off () method removes the event handler for one or more events bound on the element.

The off () method is typically used to remove event handlers added by the on () method.

Since jQuery version 1.7, the off () method is a new alternative to the unbind (), die (), and undelegate () methods. This method brings a lot of convenience to API, and we recommend it, which simplifies the jQuery code base.

To remove the specified event handler, the selector string must match the parameters passed by the on () method when the event handler is added.

To add an event that runs only once and remove it, use the one () method.

The syntax is as follows:

(selector) .off (event,selector,function (eventObj), map)

The parameters are as follows:

Event is required. Specifies one or more events or namespaces to remove from the selected element. Multiple event values are separated by spaces. Must be a valid event.

Selector is optional. Specifies the selector that is initially passed to the on () method when the event handler is added.

Function (eventObj) is optional. Specifies the function to run when an event occurs.

Map specifies the event mapping ({event:function, event:function,...}), which contains one or more events to be added to the element, as well as functions to run when the event occurs.

Examples are as follows:

(document) .ready (function () {$("p") .on ("click", function () {$(this) .css ("background-color", "pink");}); $("button") .click (function () {$("p") .off ("click");});})

Click on this paragraph to change its background color.

Click the button and then click on this paragraph (the click event has been removed).

Remove click event handle

Output result:

These are all the contents of the article "how to use off in jquery". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report