jquery - ReferenceError: 调试时 $ 未定义

标签 jquery visual-studio-code referenceerror

使用 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/

相关文章:

javascript - 在粘贴事件中获取文档中粘贴的内容

ide - VS Code search.exclude不起作用

visual-studio-code - VSCode(Visual Studio Code)可以用来跑elm-make吗?

Azure PowerShell 命令 : `Get-AzSubscription ...` - SharedTokenCacheCredential authentication failed: AADSTS9002332

javascript - Uncaught ReferenceError : Invalid left-hand side in assignment with btn button

javascript - 未捕获ReferenceError : _ is not defined and Uncaught TypeError: undefined is not a function

javascript - 使用 javascript 或 jQuery 读取 HTML 数组

javascript - Jquery Twitter 搜索 api - 没有结果

javascript - 使用 jquery 的内部 CSS

JavaScript 新函数作用域 ReferenceError