HTML一共有三种添加事件的方式:
以onclick为例
第一种:
<body>
<span onclick="test()">hello</span>
<script>
function test(){
console.log(123);
}
</script>
</body>
第二种:
<body>
<span id="name">hello</span>
<script>
document.getElementById("name").onclick=function(){
console.log(123);
}
</script>
</body>
第三种:
<body>
<span id="name">hello</span>
<script>
document.getElementById("name").addEventListener("click",function(){
console.log(123);
});
</script>
</body>