Can we call the function written in one JavaScript in another JS file?(我们可以在另一个 JS 文件中调用用一个 JavaScript 编写的函数吗?)
问题描述
我们可以在另一个JS文件中调用写在一个JS文件中的函数吗?谁能帮我如何从另一个 JS 文件中调用该函数?
Can we call the function written in one JS file in another JS file? Can anyone help me how to call the function from another JS file?
推荐答案
只要在第一次使用之前已经加载了包含函数定义的文件,就可以像在同一个JS文件中一样调用该函数函数.
The function could be called as if it was in the same JS File as long as the file containing the definition of the function has been loaded before the first use of the function.
即
文件1.js
function alertNumber(number) {
alert(number);
}
文件2.js
function alertOne() {
alertNumber("one");
}
HTML
<head>
....
<script src="File1.js" type="text/javascript"></script>
<script src="File2.js" type="text/javascript"></script>
....
</head>
<body>
....
<script type="text/javascript">
alertOne();
</script>
....
</body>
其他方式行不通.正如 Stuart Wakefield 正确指出的那样.其他方式也可以.
The other way won't work.
As correctly pointed out by Stuart Wakefield. The other way will also work.
HTML
<head>
....
<script src="File2.js" type="text/javascript"></script>
<script src="File1.js" type="text/javascript"></script>
....
</head>
<body>
....
<script type="text/javascript">
alertOne();
</script>
....
</body>
什么是行不通的:
HTML
<head>
....
<script src="File2.js" type="text/javascript"></script>
<script type="text/javascript">
alertOne();
</script>
<script src="File1.js" type="text/javascript"></script>
....
</head>
<body>
....
</body>
虽然在调用时定义了alertOne
,但在内部它使用了一个仍未定义的函数(alertNumber
).
Although alertOne
is defined when calling it, internally it uses a function that is still not defined (alertNumber
).
这篇关于我们可以在另一个 JS 文件中调用用一个 JavaScript 编写的函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我们可以在另一个 JS 文件中调用用一个 JavaScript 编写的函数吗?


基础教程推荐
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01