方式一:在事件源对象所对应的HTML标签上增加一个要处理的事件属性,属性值等于处理事件的函数名或JS代码。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JS的事件处理</title>
<script type="text/javascript">
function executeClick(){ alert("你好啊!!");}
</script>
</head>
<body>
<input type="button" value="点击" οnclick="executeClick();"/>
</body>
</html>
方法二:直接在JS代码中,设置元素对象的事件属性,让事件属性值等于处理该事件的方法名或程序代码。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JS的事件处理</title>
</head>
<body>
<input id="btn" type="button" value="点击" />
</body>
<script type="text/javascript">
document.getElementById("btn").onclick = function(){
alert("你敢点我??");
};
</script>
</html>