Get the App
SLTechnology News&Howtos  ›  Development  › 

How to cancel a click event by jquery

Shulou Source: shulou.com Published: 2022-05-31 17:13:55 09月21日 Update

This article mainly introduces the relevant knowledge of "how to cancel the click event in jquery". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope that this article "how to cancel the click event in jquery" can help you solve the problem.

There are three ways: 1. Use "Click element .unbind ()" to cancel all click events for selected elements. 2. Click the element .off () to cancel the click event added with on () in the selected element. 3. Use "parent element .undelegate ()" to cancel the click event cancelled by delegate ().

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

Jquery provides several ways to cancel a click event

1. Use the unbind () method

The unbind () method removes the event handler for the selected element.

This method can remove all or selected event handlers, or terminate the operation of the specified function when the event occurs.

This method can also unbind the event handler through the event object. This method is also used to unbind events within themselves (such as deleting event handlers after the event has been triggered a certain number of times).

Example: cancel click event

$(document) .ready (function () {$("p") .click (function () {$(this) .ready ();}) ("button") .click (function () {$("p") .unbind ();});})

This is a paragraph.

This is another paragraph.

Click on any paragraph (p element) and the paragraph will disappear.

Remove a click event on a p element

2. Use the off () method

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

$(document) .ready (function () {$("p") .on ("click", function () {$(this) .ready ();}) ("button") .click (function () {$("p") .off ();});})

3. Use the undelegate () method

The undelegate () method removes one or more event handlers added by the delegate () method.

$(document) .ready (function () {$("body") .delegate ("p", "click", function () {$(this) .ready ();}) $("button") .click (function () {$("body") .undelegate ();})

That's all for "how jquery cancels the Click event". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Events methods elements programs processes paragraphs knowledge industries this is different practical useful content function can be passed multiple practical practical object Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple MariaDB Xiaomi Redmi Huawei