将 jQuery 文件移动到头部后 $ 仍未定义

$ still not defined after moving jQuery file to head

我已经探索了在这里和其他地方找到的合理补救措施,但出于某种原因 cloud9 不想让我玩 jQuery。我从 "links at the bottom approach," 开始得到 $ not defined please fix or add /*global $*/ 错误(即使我的代码在 $(document).ready(function () {}); 中),然后将所有内容移动到 <head> 标签,这样我就没有办法了脚本可以在加载 jQuery 之前尝试 运行。仍然是同样的错误。给出了什么?

请注意,我还尝试颠倒 bootstrap 和 google jQuery 链接的顺序。此外,在前端开发方面非常新。谢谢。

<!DOCTYPE html>

<html lang="en">
    <head>
        <!-- Bootstrap Links -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

        <!-- style -->
        <style>
            .container-fluid {
                font-family: Serif, Helvetica;
                background-color: #8A2BE2;
                color: #FFFFFF;
            }

            #container-top {
                height: 75vh;
                margin: 50px 50px 50px 50px;
                background-color: #000000;
                color: #FFFFFF;
                font-size: 50px;
            }

            /* word-wrap added to ensure quote remained in container */
            .quote {
                font-family: Serif, Helvetica;
                word-wrap: break-word;
            }

            /*
            .vertical-align {
                display: flex;
                align-items: center;

            }
            */

        </style>
        <!-- scripts -->
        <script>
            $(document).ready(function () {

            });

        </script>
    </head>
    <body>
        <div class="container-fluid">

jQuery 引用 总是 需要包含 before 依赖它的插件。

这是您在问题中提供的解决方案,已添加为 SO 片段,因此您可以亲眼看到,如果以正确的顺序添加引用,它应该 有效。

$(document).ready(function() {
  $(".container-fluid").append("jquery is set up correctly");
});
.container-fluid {
  font-family: Serif, Helvetica;
  background-color: #8A2BE2;
  color: #FFFFFF;
}

#container-top {
  height: 75vh;
  margin: 50px 50px 50px 50px;
  background-color: #000000;
  color: #FFFFFF;
  font-size: 50px;
}

/* word-wrap added to ensure quote remained in container */
.quote {
  font-family: Serif, Helvetica;
  word-wrap: break-word;
}

/*
.vertical-align {
    display: flex;
    align-items: center;

}
*/
<!-- notice that jQuery reference has been added first !-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<div class="container-fluid"></div>

got the $ not defined please fix or add /global $/ error (even though my code was in $(document).ready(function () {}); )

这意味着其他内容删除了 $ 对 jQuery 的引用。为避免在 document 就绪处理程序中出现,您可以传递显式别名:

$(document).ready(function ($) {
  // now '$' refers to jQuery inside ready handler even if '$' has been overwritten by other code
});