<ul id="ul"> <li id="goSomewhere">goSomewhere</li> <li id="doSomething">doSomething</li> <li id="sayHi">sayHi</li> </ul> <script> var ul=document.getElementById("ul"); EventUtil.addHandler(ul,"click",function(event){ event=EventUtil.getEvent(event); var target=EventUtil.getTarget(event); switch(target.id) { case "goSomewhere": location.href="https://www.baidu.com"; break; case "doSomething": document.title="I changed the title"; break; case "sayHi": alert("hi"); break; } }); </script>
核心:利用冒泡原理,将事件绑定到父元素,减少内存消耗,提升性能
利用event属性找到事件源,将事件定位到具体元素上
时间: 2024-10-10 09:58:43