事件委托原生、jQuery实现

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
li{
background:red;
margin-bottom: 3px;
}
</style>
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
</body>
<script src="https://lib.baomitu.com/jquery/2.2.4/jquery.js"></script>
<script>
// 原生
let box = document.getElementsByTagName('ul')[0]
box.onclick = function(event) {
let evt = window.event || event
let target = evt.target|| evt.srcElement;
alert(target.innerHTML)
}
// jquery
$('ul').on('click','li',function(){
alert($(this).html())
console.log($(this).text())
})
</script>
</html>
原文地址:https://www.cnblogs.com/naxiaoming/p/10130495.html