1.行内式
将单行或少量的JavaScript代码写在HTML标签的事件属性中(也就是以on开头)
使用行内式实现单击按钮弹出警告框
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button onclick="alert('行内式')">点我提示信息</button>
</body>
</html>
2.嵌入式
用<script>标签包裹JavaScript代码,可以写在<head>或者<body>中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
alert('嵌入式');
</script>
</head>
<body>
</body>
</html>
3.外部式
创建一个用“js”作为后缀名的文件,使用<script>标签中的src属性进行引入
test.js文件中的代码
alert('外部式');
index.html中的代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="test.js"></script>
</body>
</html>