What is the usage of off in jquery

WBOY
Release: 2022-04-15 18:08:54
Original
3622 people have browsed it

In jquery, the off() method is used to remove the event handler of one or more events bound to the element; it is usually used to remove the event handler added through on(), the syntax is: "Element object.off(event or namespace to remove from the selected element, selector, function to run when the event occurs, map)".

What is the usage of off in jquery

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

What is the usage of off in jquery

The off() method is used to remove the event handler of one or more events bound to the element.

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

As of jQuery version 1.7, the off() method is the new replacement for the unbind(), die(), and undelegate() methods. This method brings a lot of convenience to the API and is recommended because it simplifies the jQuery code base.

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

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

The syntax is as follows:

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

The parameters are as follows:

  • event is 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, ...}) that contains one or more events to be added to the element and run when the event occurs The function.

The example is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>123</title> 
<script src="js/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("p").on("click",function(){
    $(this).css("background-color","pink");
  });
  $("button").click(function(){
    $("p").off("click");
  });
});
</script>
</head>
<body>
<p>点击这个段落修改它的背景颜色。</p>
<p>点击一下按钮再点击这个段落( click 事件被移除 )。</p>
<button>移除 click 事件句柄</button>
</body>
</html>
Copy after login

Output result:

What is the usage of off in jquery

Related video tutorial recommendation: jQuery video Tutorial

The above is the detailed content of What is the usage of off 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 admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template