How to remove element click event in javascript

WBOY
Release: 2022-05-07 11:02:55
Original
5436 people have browsed it

Method: 1. Use the "click element object.unbind("click");" method, which can remove the event handler of the selected element; 2. Use "click element object.off(" click");" method, which can remove the event handler added through the on() method.

How to remove element click event in javascript

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

How to remove element click events in javascript

Method 1. Use the unbind() method

unbind() method to remove the event of the selected element handler.

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 example is as follows:

    

这是一个段落。

Copy after login

Output result:

How to remove element click event in javascript

##Method 2, use off() method

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.

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

The example is as follows:

    123   

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

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

Copy after login
Output result:


How to remove element click event in javascript##[Related recommendations:

javascript video tutorial

webfrontend

The above is the detailed content of How to remove element click event in javascript. 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
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!