首页 > 解决方案 > 在 node.js 服务器中找不到 Javascript 函数

问题描述

我按照这个设置了 node.js 服务器:Using node.js as a simple web server

所以我安装了serve-static npm。

然后我使用以下代码创建了一个名为 test.html 的文件

<html>
<head>
  <h2>Google</h2>
</head>
<body>
  <button onclick="test()">Click me</button>
</body>
</html>

<script src="http://code.jquery.com/jquery-3.3.1.min.js" type="text/javascript">

  function test() {
    console.log('test');
  }

</script>

当我点击按钮时,我得到:

Uncaught ReferenceError: test is not defined
at HTMLButtonElement.onclick (test.html:8)

没有别的了。这段代码看起来不错...?为什么找不到这个功能?

标签: javascriptnode.js

解决方案


如果您想在标签src之间拥有自己的功能,请删除您的部分。script

<script src="http://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
  function test() {
    console.log('test');
  }
</script>

另外,你应该把你的脚本放在你的</body>


推荐阅读