$(document).ready(function(){Uncaught ReferenceError:$未定义

Mon*_*ich 19 javascript jquery referenceerror

嗨我在使用波纹管代码时遇到"未捕获的ReferenceError:$未定义"

我目前在日志中收到以下错误.我一直在看框架中的示例,我似乎无法找到错误的位置.自从我做了任何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>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Den*_*ret 11

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

阅读那里的教程:http://docs.jquery.com/Tutorials : Getting_Started_with_jQuery 它从如何导入库开始.

  • 这不包括在浏览器中,你必须这样做.但我同意你的观点是这个库几乎是每个人都应该使用的(这在开发世界中非常罕见,我常常发现人们非常愿意添加无用的库). (2认同)

Om *_*kar 10

无需使用jQuery.noConflict和所有

试试这个:

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

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

// All your code using $

});
Run Code Online (Sandbox Code Playgroud)

如果您仍然在第87行遇到错误Uncaught reference error: jQuery is not defined,那么您需要在使用之前包含jQuery文件,您可以查看上面的答案


小智 7

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

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.0.min.js"></script>
Run Code Online (Sandbox Code Playgroud)


kir*_*nvj 5

如果您确定包含 jQuery,请尝试将$替换为jQuery,然后重试。

就像是

jQuery(document).ready(function(){..
Run Code Online (Sandbox Code Playgroud)

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