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)?