Get the App
SLTechnology News&Howtos  ›  Development  › 

How jquery removes mouse events

Shulou Source: shulou.com Published: 2022-06-01 03:09:41 09月24日 Update

This article focuses on "how to remove mouse events in jquery". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "jquery how to remove mouse events"!

Methods to remove events: 1, with unbind (), syntax "bind the element of mouse event .unbind ()"; 2, with undelegate (), syntax "bind element of mouse event .undelegate ()"; 3, with off (), syntax "bind element of mouse event .off ()".

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

Jquery's method of removing mouse events:

Method 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.

Ubind () applies to any event handler attached through jQuery.

Example:

$(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 to make it disappear. Including this paragraph.

Delete the event handler for the p element

Method 2: use the undelegate () method

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

Example:

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

This is a paragraph.

This is another paragraph.

Click on any paragraph to make it disappear. Including this paragraph.

Delete click event

Method 3: 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) .css ("background-color", "pink");}) ("button") .click (function () {$("p") .off ();});})

Click on this paragraph to change its background color.

Click the button and then click on this paragraph (the click event has been removed).

Remove the click event

Note: since jQuery version 1.7, the off () method is a new alternative to the unbind (), die (), and undelegate () methods. This method brings a lot of convenience to API, and we recommend it, which simplifies the jQuery code base.

At this point, I believe you have a deeper understanding of "how to remove mouse events in jquery". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Methods events paragraphs mouse processing elements programs this is syntax content versions examples learning practical deeper code interests functions handlers multiple Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology NVidia Shulou Tech Info MySQL OPPO Reno