第一种方法:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script>
function test(){
console.log("HelloWorld")
}
</script>
</head>
<body>
<input type="button" value=按钮 id="button" onclick="test()"/>
</body>
</html>
第二种方式:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="button" value=按钮 id="button"/>
<script>
document.getElementById("button").onclick=function(){
console.log("HelloWorld")
}
</script>
</body>
</html>
第三种方式:
<!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>