事件委托是把元素事件绑定在其父元素上面代为执行,从而减少页面上的绑定次数,从而提高页面性能;
<body>
<ul id="color-list">
<li>red</li>
<li>yellow</li>
<li>blue</li>
<li>green</li>
<li>black</li>
<li>white</li>
</ul>
<script>
let colorList = document.getElementById("color-list");
colorList.addEventListener("click", function (event) {
if (event.target.nodeName === 'LI') {
alert('你点击的是li');
}
})
</script>
</body>