使用 vscode 内置调试器 node.js 时,我收到此错误消息 ReferenceError: $ is not Defined Here is the html
<!doctype html>
<html lang="en">
<head>
<title>14. Getting Started with jQuery</title>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript" src="js/app.js"></script>
</body>
</html>
这是app.js
$(function() {
// start up code goes here
alert("this works!");
});
我在警报行放置了一个断点,并在 vscode 中运行调试(node.js)。它停在 $(function() { - app.js 的第一行,错误消息为 ReferenceError: $ is not Defined。似乎 jQuery 没有加载。
我试过了
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
和
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
但它们都不起作用。请帮忙。
最佳答案
如果你想在NodeJS中使用HTML文件中加载的jQuery,你需要先将其与$
关联起来:
转到要使用它的脚本,然后写入:
window.$ = window.jQuery;
如果这不起作用,请安装 jQuery npm 包,方法是在脚本文件夹中打开终端,然后键入:
npm i jquery
然后写
window.$ = window.jQuery = require("jquery");
在你的脚本中。
关于jquery - ReferenceError: 调试时 $ 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48457867/