[英]Uncaught ReferenceError: $ is not defined Error
我得到这个错误
未捕获的ReferenceError:未定义$。
我进行了搜索,发现通常的原因不是在正确的位置声明了jQuery,但我认为我做对了。
提前致谢。
<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);
});
解决方案:
1)使用Google CDN加载jQuery
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
2)它与jQuery v1.5-v1.7兼容,因为大多数方法在jQuery 1.8+中已弃用。 这就是我在第1点使用Google CDN jquery v1.5的原因。关于循环插件的大多数示例都使用jquery 1.5。
3)清除浏览器缓存,这在大多数情况下是主要原因。
4)请使用以下代码检查jquery的加载情况
if(typeof jQuery!=='undefined'){
console.log('jQuery Loaded');
}
else{
console.log('not loaded yet');
}
可能您在某个地方调用了jQuery.noConflict()
。
要解决此问题,请像下面这样包装代码:
(function($, undefined){
if( !$ )
{
console.log('jQuery failed to load');
return;
}
//jquery code here
})(window.jQuery);
这将防止jQuery.noConflict()
影响您的代码,并且可以在jQuery遇到问题时进行调试。
另外,在Element Inspector上观看“ Network”选项卡(按F12 )。
如果脚本标记中提到的位置中不存在文件“ jquery-1.2.6.min.js”,则不会发生此错误。 根据您的代码,jquery文件应与启动页面位于同一文件夹中。
发生此错误时,我将检查以下内容
1)确保jquery脚本文件位于正确的位置
2)jQuery文件的名称与脚本标记中引用的名称相同
3)在浏览器控制台窗口中,有一个调试器或“源”选项卡,显示该页面使用的所有脚本和CSS文件。 检查浏览器的“源”选项卡下是否存在jquery
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.