给元素添加事件即将元素与函数绑定来实现功能
第一种方式:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="button" id="bt" value="按钮" onclick="test()" />
<script>
function test(){
console.log("HelloWorld");
}
</script>
</body>
</html>
第二种方式:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="button" id="botton" value="按钮" />
<script>
document.getElementById("botton").onclick=function(){
console.log("HelloWorld");
}
</script>
</body>
</html>
第三种方式:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="button" id="botton" value="按钮" />
<script>
document.getElementById("botton").addEventListener("click",function(){
console.log("HelloWorld");
})
</script>
</body>
</html>