给ui下面的li绑定事件
# 给下面的每个li绑定点击事件 点击打印的结果分别是 1 2 3(提示:事件委托)
<ul>
<li>1<li>
<li>2<li>
<li>3<li>
</ul>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<ul>
<li id="first">1</li>
<li id="second">2</li>
<li id="third">3</li>
</ul>
<script>
var ul = document.querySelector('ul');
ul.addEventListener('click', function (e) {
var target = e.target;
switch (target.id) {
case 'first':
console.log(1);
break;
case 'second':
console.log(2);
break;
case 'third':
console.log(3);
break;
}
})
</script>
</body>
</html>
运行结果