<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Event delegate</title> <style>ul{padding:20px;List-style:None; } </style></Head><Body> <ulID= "ul"> <Li>111</Li> <Li>222</Li> <Li>333</Li> <Li>444</Li> </ul> <Script> varul=document.getElementById ('ul'); Ul.onmouseover= function(EV) {varEV=EV||window.event, Target=Ev.target||ev.srcelement; //GetEvent Source if(Target.nodeName.toLowerCase ()== "Li") {Target.style.background= "Red"; }} ul.onmouseout= function(EV) {varEV=EV||window.event, Target=Ev.target||ev.srcelement; if(Target.nodeName.toLowerCase ()== "Li") {Target.style.background= ""; } } </Script></Body></HTML>
Event delegation: Use event bubbling to add events to the parent or ancestor elements to trigger the execution effect.
Benefits:
1. Improve performance.
2. Future additions to the element, the body can still trigger events.
JavaScript-Event Delegation