第一种方法
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加事件</title>
</head>
<script>
function test(){
console.log("你按我干嘛!");
}
</script>
<body>
<input type="button" value="按钮" onclick="test()" />
</body>
</html>
第二种
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加事件</title>
</head>
<body>
<input type="button" value="按钮" id="botten" />
<script>
document.getElementById("botten").onclick=function(){
console.log("你按我干嘛!");
}
</script>
</body>
</html>
第三种
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加事件</title>
</head>
<body>
<input type="button" value="按钮" id="botten" />
<script>
document.getElementById("botten").addEventListener("click",function(){
console.log("你按我干嘛!");
});
</script>
</body>
</html>