未捕获的ReferenceError:$未定义错误

时间:2022-11-07 07:33:07

I am getting this err

我错了

Uncaught ReferenceError: $ is not defined.

未捕获的ReferenceError:$未定义。

I have searched and found that the usual cause of this is not having jQuery declared in the right place but I think I have it right.

我已经搜索过,发现通常的原因是没有在正确的位置声明jQuery,但我认为我说得对。

Thanks in advance.

提前致谢。

<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<script type="text/javascript" src="ajax2.js"></script>

<script type="text/javascript">

var slideimages = new Array() // create new array to preload images
slideimages[0] = new Image() // create new instance of image object
slideimages[0].src = "images/aib.jpg" // set image src property to image path, preloading image in the process
slideimages[1] = new Image()
slideimages[1].src = "images/paddypower.jpg"
slideimages[2] = new Image()
slideimages[2].src = "images/rtegaa.jpg"
slideimages[3] = new Image()
slideimages[3].src = "images/ssgaa.jpg"

$(document).ready(function(){

    function get_weather()
    {
        var p = $("#code").val();

        var u = ($('#u').attr('checked')) ? '&u=c' : '';
        var to_load = 'get_weather.php?p='+ p + u;

        $("#weather").html('<img style="margin-top: 104px;" src="ajax-loader.gif" align="absmiddle">');

        $("#weather").load(to_load);
    }

    $(window).load(get_weather); // Trigger "get_weather" when the window loads

    // Trigger "get_weather" when the form objects are used
    $("#code").change(get_weather);
    $("#u").click(get_weather); 
});

3 个解决方案

#1


The solution:

1) Use Google CDN to load jQuery

1)使用Google CDN加载jQuery

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

2) It is compatible with jQuery v1.5-v1.7 because most of the methods were deprecated in jQuery 1.8+. This is the reason I've used v1.5 of Google CDN jquery in point 1. Most of the examples on cycle plugin use jquery 1.5.

2)它与jQuery v1.5-v1.7兼容,因为大多数方法在jQuery 1.8+中已弃用。这就是我在第1点中使用了Google CDN jquery的v1.5的原因。循环插件中的大多数示例都使用jquery 1.5。

3) Clear Your browser cache, It is the main culprit most of the times.

3)清除你的浏览器缓存,这是大多数时候的主要罪魁祸首。

4) PLease check the loading of jquery using the code below

4)使用下面的代码检查jquery的加载

if(typeof jQuery!=='undefined'){
    console.log('jQuery Loaded');
}
else{
    console.log('not loaded yet');
}

#2


Probably you called jQuery.noConflict() somewhere.

可能你在某个地方调用了jQuery.noConflict()。

To solve this, wrap your code like this:

要解决此问题,请将代码包装为:

(function($, undefined){

    if( !$ )
    {
        console.log('jQuery failed to load');
        return;
    }

    //jquery code here

})(window.jQuery);

This will prevent the jQuery.noConflict() from affecting your code and you can debug in case jQuery is having some troubles.

这将阻止jQuery.noConflict()影响您的代码,并且您可以在jQuery遇到麻烦的情况下进行调试。

Also, watch your 'Network' tab, on the Element Inspector (press F12).

另外,在Element Inspector上观看“网络”标签(按F12)。

#3


This error will not occur if the file "jquery-1.2.6.min.js" is not present in the location mentioned in your script tag. From your code, the jquery file should be present in the same folder as your startup page.

如果脚本标记中提到的位置中不存在文件“jquery-1.2.6.min.js”,则不会发生此错误。从您的代码中,jquery文件应与启动页面位于同一文件夹中。

I'll check the following things when this error occurs

发生此错误时,我会检查以下内容

1) Make sure the jquery script file is present in the correct location

1)确保jquery脚本文件存在于正确的位置

2) Name of the jquery file is same as referenced in script tag

2)jquery文件的名称与script标签中引用的名称相同

3) In browsers console window, there is a debugger or source tab which shows all the script and CSS files used by the page. Check jquery is present under the sources tab of the browser

3)在浏览器控制台窗口中,有一个调试器或源选项卡,显示页面使用的所有脚本和CSS文件。检查jquery是否存在于浏览器的sources选项卡下

#1


The solution:

1) Use Google CDN to load jQuery

1)使用Google CDN加载jQuery

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

2) It is compatible with jQuery v1.5-v1.7 because most of the methods were deprecated in jQuery 1.8+. This is the reason I've used v1.5 of Google CDN jquery in point 1. Most of the examples on cycle plugin use jquery 1.5.

2)它与jQuery v1.5-v1.7兼容,因为大多数方法在jQuery 1.8+中已弃用。这就是我在第1点中使用了Google CDN jquery的v1.5的原因。循环插件中的大多数示例都使用jquery 1.5。

3) Clear Your browser cache, It is the main culprit most of the times.

3)清除你的浏览器缓存,这是大多数时候的主要罪魁祸首。

4) PLease check the loading of jquery using the code below

4)使用下面的代码检查jquery的加载

if(typeof jQuery!=='undefined'){
    console.log('jQuery Loaded');
}
else{
    console.log('not loaded yet');
}

#2


Probably you called jQuery.noConflict() somewhere.

可能你在某个地方调用了jQuery.noConflict()。

To solve this, wrap your code like this:

要解决此问题,请将代码包装为:

(function($, undefined){

    if( !$ )
    {
        console.log('jQuery failed to load');
        return;
    }

    //jquery code here

})(window.jQuery);

This will prevent the jQuery.noConflict() from affecting your code and you can debug in case jQuery is having some troubles.

这将阻止jQuery.noConflict()影响您的代码,并且您可以在jQuery遇到麻烦的情况下进行调试。

Also, watch your 'Network' tab, on the Element Inspector (press F12).

另外,在Element Inspector上观看“网络”标签(按F12)。

#3


This error will not occur if the file "jquery-1.2.6.min.js" is not present in the location mentioned in your script tag. From your code, the jquery file should be present in the same folder as your startup page.

如果脚本标记中提到的位置中不存在文件“jquery-1.2.6.min.js”,则不会发生此错误。从您的代码中,jquery文件应与启动页面位于同一文件夹中。

I'll check the following things when this error occurs

发生此错误时,我会检查以下内容

1) Make sure the jquery script file is present in the correct location

1)确保jquery脚本文件存在于正确的位置

2) Name of the jquery file is same as referenced in script tag

2)jquery文件的名称与script标签中引用的名称相同

3) In browsers console window, there is a debugger or source tab which shows all the script and CSS files used by the page. Check jquery is present under the sources tab of the browser

3)在浏览器控制台窗口中,有一个调试器或源选项卡,显示页面使用的所有脚本和CSS文件。检查jquery是否存在于浏览器的sources选项卡下