What are the principles and pros and cons of event delegates (event proxies)?

Source: Internet
Author: User

Event Delegation Principle : event bubbling mechanism.
Pros : 1. You can save a lot of memory footprint and reduce event registration. For example, UL agent all Li's Click event is very good.
2. Can be implemented when new sub-objects, no longer need to be bound to the event, the dynamic Content section is particularly appropriate
cons : The common application of event agents should be limited to the above requirements, and if you use event proxies for all events, event errors may occur. Events that should not have been triggered are bound to events.

Look at the following example:
1<ul id= "UL1" >2<li>111</li>3<li>222</li>4<li>333</li>5</ul>6 7<script>8     varOUL1 = document.getElementById (' ul1 '));9Myaddevent (OUL1, ' click ',function(e) {Ten         varE = e | |window.event; One         vartarget = E.target | |e.srcelement; A         if(Target.nodename = = = ' LI '){ - alert (target.innerhtml); -Target.style.background = ' Red '; the         } -     }); -     //event binding encapsulated into JS function -     functionmyaddevent (obj, Ev, FN) { +         if(obj.attachevent) {//IE -Obj.attachevent (' on ' +ev, FN); +}Else{ AObj.addeventlistener (EV, FN,false); at         } -     } -      -      -</script>

What are the principles and pros and cons of event delegates (event proxies)?

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.