<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<!-- 写在元素前面,要用onload -->
<script type="text/javascript">
window.onload=function(){
var oBtn=document.getElementById("btn")
function click(){
alert("我被点击了")
}
oBtn.onclick=click;
// 按钮绑定函数不需要括号,否则函数会一打开页面就执行
}
</script>
</head>
<body>
<button id="btn">按钮</button>
</body>
</html>