1.内部JS
1.1语法
在html里增加<script>
标签,把js代码写在标签体里
<script>
//在这里写js代码
</script>
ex:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js引入方式-内部js</title>
<script>
//操作浏览器弹窗
alert("hello, world");
</script>
</head>
<body>
</body>
</html>
结果展示:
2.外部JS
1.语法
- 把js代码写在单独的js文件中,js文件后缀名是
.js
- 在HTML里使用
<script>
标签引入外部js文件
<script src="js文件的路径"></script>
3.注意事项
一个script
标签,不能既引入外部js文件,又在标签体内写js代码。
错误演示:
<script src="../js/my.js">
alert("hello");
</script>
正确演示:
<script src="../js/my.js"></script>
<script>
alert("hello");
</script>