1.直接通过onclick属性绑定函数
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script>
function test(){
console.log("HelloWorld");
}
</script>
</head>
<body>
<input type="button" value="按钮" onclick="test()" />
</body>
</html>
2.通过id调用onclick进而调用相应函数
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="button" value="按钮" onclick="test()" />
<script>
document.getElementById("click").onclick=function(){
console.log("HelloWorld");
}
</script>
</body>
</html>
3.添加事件监听
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="button" value="按钮" id="button"/>
<script>
document.getElementById("button").addEventListener("click",function(){
console.log("HelloWorld");
});
</script>
</body>
</html>