JavaScript event entrusting Technology example analysis _ javascript skills

Source: Internet
Author: User
This article mainly introduces the JavaScript event delegation technology. The example analyzes the principle, applicability, and usage of event delegation technology. If you need it, you can refer to the example below to analyze the JavaScript event delegation technology. Share it with you for your reference. The specific analysis is as follows:

If a page contains a large number of buttons, We need to bind an event handler for each button. This will affect the performance.

First, every function is an object, and the object will occupy a lot of memory. The more objects in the memory, the worse the performance.

Second, the increasing number of dom visits will lead to the delay in loading pages. In fact, there is still a good solution for how to make good use of the event processing program.

Event Delegate:

The solution to the problem of too many Event Handlers is the event entrusting technology.

Event delegation uses event bubbling. You only need to specify an event handler.

We can bind an event handler to the parent element of an event to be triggered.

 
 
  • sdsdsd
  • sdsdsd
  • sdsdsd

Now we need to bind the event handler for the three li instances ..

You only need to bind the event handler in ul.

Obj. eventHandler ($ ("mylist"), "click", function (e) {e = e | window. event; switch(e.tar get. id) {// you should remember that target is the event target. // the corresponding alert will pop up when you click the event target element. case "li_1": alert ("li_1"); break; case "li_2": alert ("li_2"); break; case "li_3": alert ("li_3 "); break }})

In a complex web application, such event delegation is very practical.

If this method is not used, bind events one by one, which means there are countless event handlers.

I hope this article will help you design javascript programs.

Related Article

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.