Home Web Front-end JS Tutorial How to use jquery's new binding event mechanism on method_jquery

How to use jquery's new binding event mechanism on method_jquery

May 16, 2016 pm 04:52 PM
Binding events

Browsing jQuery’s deprecated list, I found live() and die() in it. I quickly took a look and found that starting from jQuery 1.7, jQuery has introduced a new event binding mechanism, on() and off() The two functions handle event binding uniformly. Because there were bind(), live(), delegate() and other methods to handle event binding before, jQuery decided to launch a new function to unify the event binding methods and replace the previous methods from the aspects of performance optimization and method unification. .

on(events,[selector],[data],fn)

events: One or more event types separated by spaces and an optional namespace, such as "click" or "keydown.myPlugin".
selector: A selector string for the descendants of the selector element that triggers the filter event. If the selector is null or omitted, the event is always fired when it reaches the selected element.
data: When an event is triggered, event.data must be passed to the event processing function.
fn: The function executed when the event is triggered. The false value can also be used as a shorthand for a function that returns false.

Replace bind()
When the second parameter 'selector' is null, there is basically no difference in usage between on() and bind(), so we can think that on() is just better than bind () has an optional 'selector' parameter, so on() can easily replace bind()

Replace live()
Before 1.4, I believe everyone liked to use live() because it can bind events to current and future elements. Of course, after 1.4, delegate() can also do similar things. Something happened. The principle of live() is very simple. It delegates events through the document, so we can also use on() to achieve the same effect as live() by binding the event to the document.

Live() writing method

Copy code The code is as follows:

$('#list li').live('click', '#list li', function() {
//function code here.
});

on() is written as

Copy code The code is as follows:

$(document). on('click', '#list li', function() {
//function code here.
});

The key here is that the second parameter 'selector' is in effect. It acts as a filter, and only descendant elements of the selected element will trigger the event.

Replace delegate()
delegate() was introduced in 1.4, with the purpose of delegating event binding issues of descendant elements through ancestor elements. To some extent, it has similar advantages to live(). It's just that live() is delegated through the document element, and delegate can be any ancestor node. The writing method of using on() to implement the proxy is basically the same as that of delegate().

How to write delegate()

Copy code The code is as follows:

$('# list').delegate('li', 'click', function() {
//function code here.
});

on() writing method

Copy code The code is as follows:

$('#list ').on('click', 'li', function() {
//function code here.
});

It seems that the order of the first and second parameters is reversed, but everything else is basically the same.

Summary
jQuery launched on() for two purposes, one is to unify the interface, and the other is to improve performance, so from now on, use on() to replace bind(), live(), and delegate. In particular, don't use live() anymore, because it is already on the deprecated list and will be killed at any time. If you only bind the event once, then use one(), there is no change in this.

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Replace String Characters in JavaScript

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

Custom Google Search API Setup Tutorial

Example Colors JSON File Example Colors JSON File Mar 03, 2025 am 12:35 AM

Example Colors JSON File

8 Stunning jQuery Page Layout Plugins 8 Stunning jQuery Page Layout Plugins Mar 06, 2025 am 12:48 AM

8 Stunning jQuery Page Layout Plugins

10 jQuery Syntax Highlighters 10 jQuery Syntax Highlighters Mar 02, 2025 am 12:32 AM

10 jQuery Syntax Highlighters

Build Your Own AJAX Web Applications Build Your Own AJAX Web Applications Mar 09, 2025 am 12:11 AM

Build Your Own AJAX Web Applications

What is 'this' in JavaScript? What is 'this' in JavaScript? Mar 04, 2025 am 01:15 AM

What is 'this' in JavaScript?

10  JavaScript & jQuery MVC Tutorials 10 JavaScript & jQuery MVC Tutorials Mar 02, 2025 am 01:16 AM

10 JavaScript & jQuery MVC Tutorials

See all articles