简体   繁体   English

ReferenceError: $ 未定义

[英]ReferenceError: $ is not defined

I have this error message ReferenceError: $ is not defined我有这个错误信息ReferenceError: $ is not defined
This is my header.这是我的标题。

<link href="css/global-style.css" rel="stylesheet" type="text/css" media="screen">
<link rel="stylesheet" type="text/css" media="screen" href="css/datepicker3.css">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="assets/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/bootstrap-datepicker.js"></script>

Following is my JavaScript code以下是我的 JavaScript 代码

<script type="text/javascript">
$('#sandbox-container .input-daterange').datepicker({
    startDate: "today",
    calendarWeeks: true,
    todayHighlight: true
});
</script>

Following is the HTML以下是 HTML

<div class="col-md-12" id="sandbox-container">
    <label>Project Duration</label>
    <div class="input-daterange input-group" id="datepicker">
            <input type="text" class="input-md form-control" name="start" />
            <span class="input-group-addon">to</span>
            <input type="text" class="input-md form-control" name="end" />
    </div>
</div>

I want to show datepicker on the input tag.我想在输入标签上显示datepicker
I am using Bootstrap V3.1.我正在使用 Bootstrap V3.1。
I am using this datepicker .我正在使用这个datepicker

在使用 $ 或 jQuery 的脚本之前添加 jQuery 库以便可以在脚本中识别 $。

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>

使用 Google CDN 进行快速加载:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

在 head 标签中添加这个脚本:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

It's one of the common mistake everybody make while working with jQuery, Basically $ is an alias of jQuery() so when you try to call/access it before declaring the function will endup throwing this error.这是每个人在使用 jQuery 时都会犯的常见错误之一,基本上 $ 是 jQuery() 的别名,因此当您在声明函数之前尝试调用/访问它时,最终会抛出此错误。

Reasons might be原因可能是

1) Path to jQuery library you included is not correct. 1) 您包含的 jQuery 库的路径不正确。

2) Added library after the script were you see that error 2)在脚本之后添加了库,你看到了那个错误

To solve this为了解决这个

Load the jquery library beginning of all your javascript files/scripts.在所有 javascript 文件/脚本的开头加载 jquery 库。

<script src="https://code.jquery.com/jquery-3.3.1.js"></script>

jQuery is a JavaScript library, The purpose of jQuery is to make code much easier to use JavaScript. jQuery 是一个 JavaScript 库,jQuery 的目的是让代码更容易使用 JavaScript。

The jQuery syntax is tailor-made for selecting, A $ sign to define/access jQuery. jQuery 语法是为选择量身定制的,一个$符号来定义/访问 jQuery。

Its in declaration sequence must be on top then any other script included which uses jQuery它的声明序列必须在上面,然后包含使用 jQuery 的任何其他脚本

Correct position to jQuery declaration : jQuery 声明的正确位置:

 $(document).ready(function(){ console.log('hi from jQuery!'); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/js/bootstrap-datepicker.js"></script>

Above example will work perfectly because jQuery library is initialized before any other library which is using jQuery functions, including $上面的示例将完美运行,因为 jQuery 库在使用 jQuery 函数的任何其他库之前初始化,包括$

But if you apply it somewhere else, jQuery functions will not initialize in browser DOM and it will not able to identify any code related to jQuery, and its code starts with $ sign, so you will receive $ is not a function error.但是如果你在其他地方应用它,jQuery 函数将不会在浏览器 DOM 中初始化,它也无法识别任何与 jQuery 相关的代码,并且它的代码以$符号开头,所以你会收到$ is not a function错误。

Incorrect position for jQuery declaration: jQuery 声明的位置不正确:

 $(document).ready(function(){ console.log('hi from jQuery!'); });
 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/js/bootstrap-datepicker.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Above code will not work because, jQuery is not declared on the top of any library which uses jQuery ready function.上面的代码将不起作用,因为 jQuery 没有在任何使用 jQuery 就绪函数的库的顶部声明。

Though my response is late, but it can still help.虽然我的回复晚了,但它仍然可以提供帮助。

lf you are using Spring Tool Suite and you still think that the JQuery file reference path is correct, then refresh your project whenever you modify the JQuery file.如果你使用的是 Spring Tool Suite 并且你仍然认为 JQuery 文件引用路径是正确的,那么每当你修改 JQuery 文件时刷新你的项目。

You refresh by right-clicking on the project name -> refresh.您可以通过右键单击项目名称-> 刷新来刷新。

That's what solved mine.这就是我的解决方案。

这可能是由于两个不同jquery版本的jquery冲突使用noconflict方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM