一、直接在事件中写,事件名=“js代码”
<input type="button" value="点我" οnclick="alert('aaaaaaaaaaa')"/>
二、首先用户通过function去定义一个函数,其次调用:事件名="自定义函数名()"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<title>Title</title>
<script>
function show(){
alert('直角三角形');
}
</script>
</head>
<body>
<input type="button" value="三角形" onclick="show()"/>
</body>
</html>
三、引入外部js文件,注意,内部不能写js代码,写了也没用
show.js
function show(){
alert('正方形');
}
Demo1.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<script type="text/javascript" src="../js/show.js"></script>
</head>
<body>
<input type="button" value="四边形" onclick="show()"/>
</body>
</html>