1、第一种方式:直接添加
<!DOCTYPE html>
<html>
<head>
<script>
<meta charset="UTF-8">
<title></title>
function test(){
console.log("1");
}
</script>
</head>
<body>
<input type="button" value="按钮" onclick="test()" />
</body>
</html>
2、第二种方式
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="button" value="按钮" onclick="test()" />
<script>
document.getElementById("button").onclick=function(){
console.log("1");
}
</script>
</body>
</html>
3、第三种方法
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="button" value="按钮" onclick="test()" />
<script>
document.getElementById("button").addEventListener("click",function(){
console.log("1");
});
</script>
</body>
</html>