Home >Web Front-end >Front-end Q&A >How to use the unbind() method of jquery event

How to use the unbind() method of jquery event

WBOY
WBOYOriginal
2022-06-01 18:52:142293browse

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)
  • 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>

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!

Statement:
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