<html> <head> <title>HTML事件流</title> <script> var btn=document.getElementById("btn"); btn.onclick=showMessage(); function showMessage(){ alert("測試"); } </script> </head> <body> <div> <input type="button" id ="btn" value="測試"/> </div> </body> </html>
btn.onclick=showMessage();這樣寫不行啊,會在剛進畫面時彈框,點擊按鈕沒有反應.
2014-08-29
<html>
<head>
<title>HTML事件流</title>
</head>
<body>
<div>
<input type="button" id ="btn" value="測試"/>
<script>
var btn=document.getElementById("btn");
btn.onclick=showMessage;
function showMessage(){
alert("測試");
}
</script>
</div>
</body>
</html>
還需要把JS代碼寫到相應id的元素下方
2014-08-29
btn.onclick=showMessage;這樣寫就好了,不用家調用方法后面的括號