Javascript $(document).ready(function(){ Uncaught ReferenceError: $ is not defined

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10864833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:18:34  来源:igfitidea点击:

$(document).ready(function(){ Uncaught ReferenceError: $ is not defined

javascriptjqueryreferenceerror

提问by Monika Milosavich

Hi I am having a "Uncaught ReferenceError: $ is not defined" while using bellow codes

嗨,我在使用波纹管代码时遇到“未捕获的引用错误:未定义 $”

I am currently getting the following error in my log. I have been looking at the samples in the framework and I just can't seem to find where the error is. It's been over a decade since I have done any HTML or js and what I did back then was very basic stuff. Any help would be appreciated

我目前在我的日志中收到以下错误。我一直在查看框架中的示例,但似乎无法找到错误所在。我已经有十多年没有做过任何 HTML 或 js 了,那时我所做的都是非常基本的东西。任何帮助,将不胜感激

<script type="text/javascript">
var sQuery = '<?php echo $sQuery; ?>';

$(document).ready(function(){
    if($('input[name=sPattern]').val() == sQuery) {
        $('input[name=sPattern]').css('color', 'gray');
    }
    $('input[name=sPattern]').click(function(){
        if($('input[name=sPattern]').val() == sQuery) {
            $('input[name=sPattern]').val('');
            $('input[name=sPattern]').css('color', '');
        }
    });
    $('input[name=sPattern]').blur(function(){
        if($('input[name=sPattern]').val() == '') {
            $('input[name=sPattern]').val(sQuery);
            $('input[name=sPattern]').css('color', 'gray');
        }
    });
    $('input[name=sPattern]').keypress(function(){
        $('input[name=sPattern]').css('background','');
    })
});
function doSearch() {
    if($('input[name=sPattern]').val() == sQuery){
        return false;
    }
    if($('input[name=sPattern]').val().length < 3) {
        $('input[name=sPattern]').css('background', '#FFC6C6');
        return false;
    }
    return true;
}
</script>

enter image description here

在此处输入图片说明

回答by Denys Séguret

It seems you don't import jquery. Those $ functions come with this non standard (but very useful) library.

看来您没有导入jquery。这些 $ 函数带有这个非标准(但非常有用)的库。

Read the tutorial there : http://docs.jquery.com/Tutorials:Getting_Started_with_jQueryIt starts with how to import the library.

阅读那里的教程:http: //docs.jquery.com/Tutorials: Getting_Started_with_jQuery首先介绍如何导入库。

回答by Om Shankar

No need to use jQuery.noConflictand all

无需使用jQuery.noConflict和所有

Try this instead:

试试这个:

// Replace line no. 87 (guessing from your chrome console) to the following

jQuery(document).ready(function($){

// All your code using $

});

If you still get error at line 87, like Uncaught reference error: jQuery is not defined, then you need to include jQuery file before using it, for which you can check the above answers

如果你仍然在第 87 行出现错误,比如Uncaught reference error: jQuery is not defined,那么你需要在使用它之前包含 jQuery 文件,你可以检查上面的答案

回答by tmwilliamlin

Put this code in the <head></head>tags:

将此代码放在<head></head>标签中:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.0.min.js"></script>

回答by kiranvj

If you are sure jQuery is included try replacing $with jQueryand try again.

如果您确定包含jQuery,请尝试用jQuery替换$并重试。

Something like

就像是

jQuery(document).ready(function(){..

Still if you are getting error, you haven't included jQuery.

如果出现错误,则说明您还没有包含 jQuery。

回答by Raul Reyes

I know this is an old question, and most people have replied with good answers. But for reference and hopefully saving somebody else's time. Check if your function:

我知道这是一个老问题,大多数人都给出了很好的答案。但作为参考,希望能节省别人的时间。检查您的功能是否:

$(document).ready(function(){}

is being called afteryou have loaded the JQuery library

加载 JQuery 库 调用

回答by maksbd19

many other people answered your question above. This problen arises when your script don't find the jQuery script and if you are using other framework or cms then maybe there is a conflict between jQuery and other libraries. In my case i used as following- `

许多其他人在上面回答了您的问题。当您的脚本找不到 jQuery 脚本并且如果您使用其他框架或 cms 时,就会出现此问题,那么 jQuery 和其他库之间可能存在冲突。在我的情况下,我使用如下-`

<script src="js_directory/jquery.1.7.min.js"></script>
    <script>
    jQuery.noConflict();
    jQuery(document).ready(
    function($){
    //your other code here
    });</script>

`

`

here might be some syntax error. Please forgive me because i'm writing from my cell phone. Thanks

这可能是一些语法错误。请原谅我,因为我是用手机写的。谢谢

回答by Revol89

Remember that you must first load jquery script and then the script js

请记住,您必须先加载 jquery 脚本,然后再加载脚本 js

<script type="text/javascript" src="http://code.jquery.com/jquery-2.2.4.min.js"></script>
<script type="text/javascript" src="example.js"></script>

Html is read sequentially!

html是顺序读取的!