【问题标题】:ReferenceError: $ is not defined when debugReferenceError:调试时未定义$
【发布时间】:2018-01-26 08:25:42
【问题描述】:

我有这个错误信息 ReferenceError: $ is not defined when using vscode built-in debugger node.js 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>        

但它们都不起作用。请帮忙。

【问题讨论】:

  • jQuery 实际上没有加载,但这是非常基本的。不知道你为什么说node.js,就是一个简单的HTML页面,不能直接打开控制台检查一下错误吗?

标签: jquery visual-studio-code referenceerror


【解决方案1】:

如果你想在NodeJS中使用你的HTML文件中加载的jQuery,你需要先把它和$关联起来:

转到你想使用它的脚本,然后写:

window.$ = window.jQuery;

如果这不起作用,请安装 jQuery npm 包,方法是在脚本文件夹中打开终端,然后键入:

npm i jquery 

然后写

window.$ = window.jQuery = require("jquery");

在您的脚本中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-21
    • 2019-09-25
    • 2018-07-20
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    • 2021-04-11
    • 2020-04-14
    相关资源
    最近更新 更多