我有此错误消息ReferenceError:使用vscode内置调试器node.js时未定义$这里是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中运行debug(node.js)。它停在$(function(){ - 带有ReferenceError错误消息的app.js的第一行:$未定义。似乎没有加载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>
但它们都不起作用。请帮忙。
答案 0 :(得分:2)
如果要在NodeJS中使用HTML文件中加载的jQuery,则需要先将其与$
关联:
转到要使用它的脚本,然后写:
window.$ = window.jQuery;
如果这不起作用,请通过在脚本文件夹中打开终端然后键入以下内容来安装jQuery npm包:
npm i jquery
然后写
window.$ = window.jQuery = require("jquery");
在你的脚本中。