Home > Web Front-end > Front-end Q&A > What are the advantages of the on method in jquery

What are the advantages of the on method in jquery

青灯夜游
Release: 2022-09-09 18:08:17
Original
1870 people have browsed it

Advantages of the on method in jquery: 1. The on() method can bind events dynamically added to page elements, and the event handlers added are applicable to current and future elements; 2. on() method Adding one or more event handlers to an element at once can improve efficiency.

What are the advantages of the on method in jquery

The operating environment of this tutorial: windows7 system, jquery3.6.1 version, Dell G3 computer.

on() method adds one or more event handlers on the selected element and sub-elements.

Since jQuery version 1.7, the on() method is the new replacement for the bind(), live(), and delegate() methods. This method brings a lot of convenience to the API and is recommended because it simplifies the jQuery code base.

It is troublesome to write separate registration events for a single element

$("p").click(function(){
  $(this).hide();
});
$("p").mouseenter(function(){
  $(this).css("background","blue");
});
Copy after login

So you can register multiple events at once through on

$("p").on({
  click:function(){
    $(this).hide();
  },
  mouseenter:function(){
    $(this).css("background","blue");
  }
});
 
//还可以合并
$("div").on( "mouseenter mouseleave", function(){
    $(this).toggleclass( "current");
})
Copy after login

The methods for binding events with jQuery are Several, it is recommended to use the .on() method for binding, for two reasons:

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

For example, for DOM elements that are dynamically added to the page, events bound with the .on() method do not need to care about when the element that registered the event is added, nor do they need to be bound repeatedly. Some students may be accustomed to using .bind(), .live() or .delegate(). If you look at the source code, you will find that they actually call the .on() method, and the .live() method is in jQuery1. Version 9 has been removed.

Event handlers added using the on() method apply to current and future elements (such as new elements created by scripts).

bind:

function(
 types, data, fn ) {
  return this.on(
 types, null,
 data, fn );
},
Copy after login

live:

function(
 types, data, fn ) {
  jQuery(
this.context
 ).on( types, this.selector,
 data, fn );
  return this;
},
Copy after login

delegate:

function(
 selector, types, data, fn ) {
  return this.on(
 types, selector, data, fn );
}
Copy after login

2, on() method once Performance Adding one or more event handlers to elements can improve efficiency

Many articles have mentioned using event bubbling and proxies to improve the efficiency of event binding, but most of them have not listed specific details. There is a difference, so in order to verify, I did a small test.

Assume that 5,000 li's are added to the page, and use chrome developer tool Profiles to test the page loading time.

  • Normal binding (let’s call it that)

$("li").click(function(){
  console.log(this)
});
Copy after login
$(document).on("click","li",function(){
  console.log(this)
})
Copy after login

Binding The execution time of the process

  • Ordinary binding is equivalent to registering click events separately on li. The memory usage is about 4.2M and the binding time is about 72ms.

  • .on() binding uses event proxy, only one click event is registered on the document, the memory usage is about 2.2M, and the binding time is about 1ms.

[Recommended learning: jQuery video tutorial, web front-end video

The above is the detailed content of What are the advantages of the on method in 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