当前位置:网站首页>JS的调用方式与执行顺序
JS的调用方式与执行顺序
2022-07-26 05:50:00 【Alkali!】
使用方式
HTML页面中的任意位置加上<script type="module"></script>标签即可。
type="module" //必须要加,这可以严格限制变量的作用域,
把每个变量严格限制在它应有的作用域内。
常见使用方式有以下几种:
- 直接在
<script type="module"></script>标签内写JS代码。 - 直接引入文件:
<script type="module" src="/static/js/index.js"></script>。 - 将所需的代码通过import关键字引入到当前作用域。
例如:/static/js/index.js文件中的内容为:
let name = "acwing";
function print() {
console.log("Hello World!");
}
export {
name,
print
}
<script type="module"></script>中的内容为:
<script type="module">
import {
name, print } from "/static/js/index.js";
console.log(name);
print();
</script>
执行顺序
- 类似于HTML与CSS,按从上到下的顺序执行;
- 事件驱动执行;
HTML, CSS, JavaScript三者之间的关系
- CSS控制HTML
- JavaScript控制HTML与CSS
- 为了方便开发与维护,尽量按照上述顺序写代码。例如:不要在HTML中调用JavaScript中的函数。
边栏推荐
- No EGL Display 报错解决
- 平衡二叉树(AVL) ~
- 程序员如何改善精神内耗?
- Ros2 preliminary: basic communication with topic
- 选电子工程被劝退,真的没前景了?
- Solve vagrant's error b:48:in `join ': incompatible character encodings: GBK and UTF-8 (encoding:: Compatib
- MBA-day29 算术-绝对值初步认识
- Realize channel routing based on policy mode
- 金仓数据库 KingbaseES SQL 语言参考手册 (10. 查询和子查询)
- 520送什么?DIY一个高颜值RGB时钟,女生看了都想要
猜你喜欢

Benji Banas launched the second season of earn while playing bonus activities, supporting the use of multiple Benji passes!

Interview questions for software testing is a collection of interview questions for senior test engineers, which is exclusive to the whole network

Redis 官方可视化工具,高颜值,功能真心强大!

Three implementation methods of thread and the usage of handler

Data warehouse construction -dim floor

某公司给每个工位装监控:只为看员工写代码?

平衡二叉树(AVL) ~

Redis transaction

How students apply for free idea

柠檬班自动化学习毕竟
随机推荐
Debugging sharp weapon! A lightweight log library log.c
em和rem
I also found excellent software and hardware projects, all open source
STL common template library
Motor control column summary
MBA-day29 算术-绝对值初步认识
Ros2 preliminary: basic communication with topic
You'd better not take this kind of project!
Rocbossphp free open source light community system
Balanced binary tree (AVL)~
Dynamic memory management and flexible array
满二叉树 / 真二叉树 / 完全二叉树 ~
Mba-day28 concept of number - exercise questions
Unity Profiler
2022 National latest fire-fighting facility operator (Senior fire-fighting facility operator) simulation test questions and answers
K. Link with Bracket Sequence I dp
Unity Profiler
Nn.moudle module - details of creating neural network structure
Is it really hopeless to choose electronic engineering and be discouraged?
ERROR: Could not open requirements file: [Errno 2] No such file or directory: ‘requirments.txt’