假设我有 first.js 文件,我在其中声明并使用有效值初始化了数组。
var arrVar = document.getElementsByTagName("label");
我想在 second.js 文件中使用这个变量“arrVar”。我怎样才能实现这个目标?
我尝试在第二个.js 文件的开头添加以下语句。
<script type="text/javascript" src="/first.js"></script>
但我收到错误“意外的 token <”
最佳答案
您无法将 HTML 标记添加到 JavaScript 文件中。
只要 arrVar
已在全局范围内定义(不在任何函数内),它就应该可以在任何其他文件中访问,因为它是全局变量。
关于javascript - JS : how to access the variable declared in one JS file in another JS file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49859584/