关于jquery:WebStorm ReferenceError:$未定义

WebStorm ReferenceError: $ is not defined

因此在WebStorm中编译时出现此错误:

(function (exports, require, module, __filename, __dirname) { $(function () {

ReferenceError: $ is not defined

似乎jQuery无法正确加载。这是我的HTML:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    Title
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
   
    <script type="text/javascript" src="AlexTypescript.js">
</head>
<body>
   
        <button id="BtnBC">
            Change BC
        </button>
   
</body>
</html>

这是我的功能:

1
2
3
4
5
$(function () {
    $("#BtnBC").on("click", function () {
        $("body").css("background-color","black");
    });
});

我创建了一个简单的按钮来检查它是否可以正常工作。这是。在WebStorm中进行编译时,我只会收到一条错误消息。

我到目前为止所做的:

1
2
3
4
1) Clean installation of the IDE, nodejs, modules etc.
2) Checking the referenced path of jQuery and that it is loaded before the .js file
3) Referencing a local jQuery installation as well as the CDN
4) Writing jQuery instead of $

我对编码还很陌生,所以我非常感谢您的帮助:)


尝试更改,美元符号($)只是jQuery的别名,其他框架和库可以使用$

1
2
3
4
5
jQuery(function ($) {
        $("#BtnBC").on("click", function () {
            $("body").css("background-color","black");
        });
    });
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    Title
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
   
    <script type="text/javascript" src="AlexTypescript.js">
</head>
<body>
   
        <button id="BtnBC">
            Change BC
        </button>
   
</body>
</html>