一:直接添加脚本
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title></head><body> <!-- alert就是js中的对话框 --> <!-- onclick被单击的动作 --> <input type="button" value="戳我呀给你惊喜" onclick='alert("我就是你的惊喜");' > </body></html>
二:使用script标记插入脚本
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <!-- 实现js的代码 --> <script type="text/javascript"> function my_button() { alert("我就是你的惊吓"); } </script> </head><body> <input type="button" value="戳我呀给你惊喜" onclick='my_button();' > </body></html>
三:链接脚本文件
index.html
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <!-- src一定要接js文件的路径 --> <script type="text/javascript" src="deal.js"> </script> </head><body> <input type="button" value="戳我呀给你惊喜" onclick='my_button();' ></body></html>
deal.js
function my_button() { alert("我是你的小可爱"); }