js绑定事件2种方法:
1. <input type = "button" onclick="clickHandler()">
2. 该方法必须掌握
<input type = "button" id="button1">
<script type = "text/javascript">
var v = document.getElementById("button1");
v.onclick=clickHandler; //注意,没有括号
</script>
<!DOCTYPE html> <html> <head> <title>js16.html</title> </head> <body> <script type="text/javascript"> function getEvent(event)//event是浏览器自动生成的 { alert("事件类型:" + event.type); } document.write("单击..."); document.onmousedown = getEvent; //这里没有括号,不可以写成 getEvent(event); </script> </body> </html>
再看一个:
<!DOCTYPE html> <html> <head> <title>js17.html</title> </head> <body> <input type="button" id="button1" value = "button1 "> <script type="text/javascript"> var v = document.getElementById("button1"); v.onclick = function clickHandler() { alert ("button is clicked."); } </script> </body> </html>
安安