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.
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 }); ```
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 }); ```
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 }); ```
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!