我们了解html 结构就知道,html 中以head 和body 两部分,script 标签出现的位置就可以在head 和body 中。
不熟悉html 基本结构的,可以参考Html5 head 与 body。
- js 代码写在head 中。
<html>
<head>
<script>
let greeting = "Hello in html head";
alert(greeting);
</script>
</head>
<body>
<p>hello js script tag in the head tag</p>
</body>
</html>
运行效果


2. js 代码写在body 中。
<html>
<head>
</head>
<body>
<p>hello js script tag in the body tag</p>
<script>
let greeting = "Hello in html body";
alert(greeting);
</script>
</body>
</html>




被折叠的 条评论
为什么被折叠?



