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.
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.