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标签中间不要写代码,否则会被忽略
相关推荐
小成2023032026519 分钟前
Linux高级02
linux·开发语言
知行合一。。。29 分钟前
Python--04--数据容器(总结)
开发语言·python
咸鱼2.042 分钟前
【java入门到放弃】需要背诵
java·开发语言
ZK_H43 分钟前
嵌入式c语言——关键字其6
c语言·开发语言·计算机网络·面试·职场和发展
A.A呐1 小时前
【C++第二十九章】IO流
开发语言·c++
椰猫子1 小时前
Java:异常(exception)
java·开发语言
lifewange1 小时前
pytest-类中测试方法、多文件批量执行
开发语言·python·pytest
GreenTea1 小时前
一文搞懂Harness Engineering与Meta-Harness
前端·人工智能·后端
cmpxr_1 小时前
【C】原码和补码以及环形坐标取模算法
c语言·开发语言·算法
2401_827499992 小时前
python项目实战09-AI智能伴侣(ai_partner_5-6)
开发语言·python