JS的三种书写位置

内部

clike 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
        alert("你好")
      </script>
</body>

</html>

外部

clike 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script src="./外部.js"></script>
</body>

</html>

行内

clike 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <h1 onclick="alert('丘丘')">锄禾日当午</h1>
</body>

</html>
  • 不管是内部的还是外部的,书写的位置都尽量写到文档末尾前面
  • 外部Js标签中间不要写代码,否则会被忽略
相关推荐
yddddddy1 小时前
css的基本知识
前端·css
昔人'1 小时前
css `lh`单位
前端·css
hdsoft_huge1 小时前
Java & Spring Boot常见异常全解析:原因、危害、处理与防范
java·开发语言·spring boot
风中的微尘1 小时前
39.网络流入门
开发语言·网络·c++·算法
前端君2 小时前
实现最大异步并发执行队列
javascript
未来之窗软件服务2 小时前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
小冯记录编程2 小时前
C++指针陷阱:高效背后的致命危险
开发语言·c++·visual studio
1uther3 小时前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎
Nan_Shu_6143 小时前
Web前端面试题(2)
前端
知识分享小能手3 小时前
React学习教程,从入门到精通,React 组件核心语法知识点详解(类组件体系)(19)
前端·javascript·vue.js·学习·react.js·react·anti-design-vue