How to use the unbind() method of jquery event

WBOY
Release: 2022-06-01 18:52:14
Original
2251 people have browsed it

In jquery, the unbind() method is used to remove the event handler of a specified element. The syntax is "element object.unbind(event to be removed, unbind function name, move to be used) event object)"; this method can remove the specified event handler, or terminate the execution of the specified function when the event occurs.

How to use the unbind() method of jquery event

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

How to use the unbind() method of jquery event

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.

The syntax is:

$(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:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>123</title> 
<script src="js/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("p").click(function(){
    $(this).slideToggle();
  });
  $("button").click(function(){
    $("p").unbind();
  });
});
</script>
</head>
<body>
<p>这是一个段落。</p>
<p>这是另外一个段落。</p>
<p>点击任意段落(p 元素),该段落就会消失。</p>
<button>移除所有段落(p 元素)的事件句柄</button>
</body>
</html>
Copy after login

Output result:

How to use the unbind() method of jquery event

Related video tutorial recommendations:jQuery video tutorial

The above is the detailed content of How to use the unbind() method of jquery 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