[英]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.