<!DOCTYPE?html>
<html>
<head>
????<meta?charset="utf-8">
????<title>事件</title>
????<script?type="text/javascript">
????????//方法一??把事件寫到window.onload里面
????????window.onload=function(){
????????????var?button?=?document.getElementById('button');
????????????button.onclick?=?function(){
????????????????alert('你點擊了我')
????????????}
????????}
????</script>
</head>
<body>
<button?id="button">點擊我</button>
</body>
</html>
<!DOCTYPE?html>
<html>
<head>
????<meta?charset="utf-8">
????<title>事件</title>
????<script?type="text/javascript">
????????function?clickMe(){
????????????alert('你點了我')
????????}
????</script>
</head>
<body>
<!--方法二??把事件直接寫到元素上-->
<button?id="button"?onclick="clickMe()">點擊我</button>
</body>
</html>
<!DOCTYPE?html>
<html>
<head>
????<meta?charset="utf-8">
????<title>事件</title>
</head>
<body>
<button?id="button">點擊我</button>
<!--方法三??把script標(biāo)簽寫到body標(biāo)簽最后-->
<script>
????var?button?=?document.getElementById('button');
????button.onclick?=?function(){
????????alert('你點擊了我')
????}
</script>
</body>
</html>
以上是不借助任何第三方工具的最常見的三種寫法