Home > Common Problem > What are the monitoring methods of jquery?

What are the monitoring methods of jquery?

尊渡假赌尊渡假赌尊渡假赌
Release: 2023-06-25 09:59:17
Original
3316 people have browsed it

There are four main listening methods of jquery: 1. ".on" method, which can monitor single or multiple events, and can also bind events to newly added elements in the future, but the old version of jQuery does not support it; 2. ".click" method, which mainly performs click event operations. It does not support event delegation and can only be applied to existing elements; 3. ".bind" method, which is provided by the old jQuery version to bind events to elements. method, used in conjunction with other jQuery methods to simplify the code, does not support event delegation.

What are the monitoring methods of jquery?

The operating system of this tutorial: Windows 10 system, jQuery3.6.0 version, Dell G3 computer.

The main methods for jQuery to listen to events are `.on()`, `.click()` and `.bind()`

1. `.on()`

`.on()` is a new method introduced in jQuery 1.7 and can be used to replace the previous method. This method can listen to single or multiple events, and can also bind events to newly added elements in the future.

Advantages

  • It is easy to add and delete events;

  • Can bind multiple events at the same time, unlike .click () can only listen to click events. For example: `$(selector).on({mouseenter: function(){}, mouseleave: function(){}})`

  • You can add a namespace;

Disadvantages

  • Older versions of jQuery do not support it;

  • May require shim decay in older browsers.

Example:

```
$('selector').on('click', function() {
  // code
});
```
Copy after login

2, `.click()`

`.click()` mainly clicks event operations.

Advantages

  • Easy to use; When you only need to listen for click events, the code will be concise and easy to understand.

Disadvantages

  • Does not support event delegation and can only be applied to existing elements;

  • Only Click event, this method cannot be bound to other events.

Example:

```
$('selector').click(function() {
  // code
});
```
Copy after login

3, `.bind()`

`.bind()` is an old version of jQuery Version provides methods for binding events to elements.

Advantages

  • When used in conjunction with other jQuery methods, the code can be simplified;

  • Yes Bind multiple events.

Disadvantages

Does not support event delegation; Only used for existing elements.

Example:

```
$('selector').bind('click', function() {
  // code
});
```
Copy after login

The above is the detailed content of What are the monitoring methods of jquery?. 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