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标签中间不要写代码,否则会被忽略
相关推荐
HSJ01701 分钟前
Aviator中使用BigDecimal进行高精度计算
java·开发语言·bigdecimal·aviator
weixin_307779135 分钟前
利用 AWS Lambda 与 EventBridge 优化低频 Java 作业的云计算成本
java·开发语言·云原生·云计算·aws
CheungChunChiu6 分钟前
从 Rust 到 Flutter:嵌入式图形与构建工具全景指南
开发语言·flutter·rust
满天星830357712 分钟前
【C++】智能指针
c语言·开发语言·c++·visual studio
布列瑟农的星空18 分钟前
后台类项目如何挖掘前端技术亮点
前端·面试
hj104341 分钟前
php上传企业微信附件的方法
开发语言·php·企业微信
wangbing11251 小时前
layui窗口标题
前端·javascript·layui
qq_398586541 小时前
Utools插件实现Web Bluetooth
前端·javascript·electron·node·web·web bluetooth
李剑一1 小时前
mitt和bus有什么区别
前端·javascript·vue.js
VisuperviReborn1 小时前
React Native 与 iOS 原生通信:从理论到实践
前端·react native·前端框架