Javascript 调用函数会导致引用错误,因为函数未定义
我有两个JS文件-Javascript 调用函数会导致引用错误,因为函数未定义,javascript,Javascript,我有两个JS文件-first.JS和second.JS 我已经在second.js中直接定义了一个函数call(),而没有将其包含在任何内容下 #second.js function call(){ console.log('output'); } 现在,当我从first.js调用它时,它给出了引用错误,因为函数call()未定义 #first.js function test(){ call(); } 我在main.html中按以下顺序调用这两个单独的js文件
first.JS
和second.JS
我已经在
second.js
中直接定义了一个函数call()
,而没有将其包含在任何内容下
#second.js
function call(){
console.log('output');
}
现在,当我从first.js调用它时,它给出了引用错误,因为函数call()
未定义
#first.js
function test(){
call();
}
我在main.html中按以下顺序调用这两个单独的js文件
<script type = "text/javascript" src="second.js"> </script>
<script type = "text/javascript" src="first.js"> </script>
既然在第一个函数之前包含第二个函数,那么函数call()
已经存在,也没有包装在任何其他函数中,为什么会出现引用错误
EDIT:-根据建议,我检查了具有导致问题的onclick函数的
second.js
,element.onclick=function(){$.get({{url\u for('python\u function')}};}
。我把它改为element.onclick=function(){$.get({{url\u for(python\u function)}}})}
,现在一切都好了 代码中有一些错误。首先,您必须将second.js
中的代码导入First.js
。看起来是这样的:
#first.js
import <name variable whatever you want> from '<path to second.js>';
function test(){
<name of the variable you made in the first line>();
}
test();
<script type = "text/javascript" src="second.js"> </script>
<script type = "module" src="first.js"> </script>
最后,在HTML中,您希望将first.js
的类型指定为模块,而不是text/javascript
,如下所示:
#first.js
import <name variable whatever you want> from '<path to second.js>';
function test(){
<name of the variable you made in the first line>();
}
test();
<script type = "text/javascript" src="second.js"> </script>
<script type = "module" src="first.js"> </script>
希望这有帮助!让我知道它是否适合您您确定第二个
加载正确吗?没有语法错误或什么?不需要,先生,我可以在控制台下看到它是唯一的错误,没有别的。我无法复制它,一定有一些代码或上下文缺失。它拼写错误,或者你真的将这些文件包含在main.JS
文件中?如果键入call(),会发生什么代码>在控制台中?它运行吗?如果您正在使用模块,则无需包含second.js
。@connexo您是对的!我将更新我的答案以反映这一点。谢谢您的帮助,先生,非常感谢!我已经做了编辑,请看你是否有时间。