JQuery's. on () method

Source: Internet
Author: User
Tags chrome developer

It's not completely clear how to use. on () for jQuery event binding first look at the http://api.jquery.com/on/ here

JQuery binds events in several ways. We recommend that you bind events using the. on () method for two reasons:

1. The on () method can bind events dynamically added to page elements.

For example, when a DOM element is dynamically added to a page, events bound using the. on () method do not need to worry about when the elements registered for this event are added or need to be bound repeatedly. Some may be used to it. bind (),. live () or. delegate (), view the source code and you will find that they actually call all of them. on () method, and. the live () method has been removed in jQuery1.9.

bind: function( types, data, fn ) {    return this.on( types, null, data, fn );},live: function( types, data, fn ) {    jQuery( this.context ).on( types, this.selector, data, fn );    return this;},delegate: function( selector, types, data, fn ) {    return this.on( types, selector, data, fn );}

Remove events bound to. on () using the. off () method.

2. Bind events using the on () method to improve efficiency

Many articles have mentioned how to use event bubbles and proxies to improve the efficiency of event binding. Most of the differences are not listed. Therefore, I will perform a small test to verify the difference.

Assume that 5000 li is added to the page and the page loading time is tested using the chrome developer tool Profiles.

Normal binding (this is what we call it)

$('li').click(function(){    console.log(this)});

Binding process execution time

Binding process execution time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.