2017-05-28 110 views
-1

我收到此錯誤,當我點擊我的HTML頁面的一個元素,應該加載一個時間表的種類。無法讀取'長度'的屬性,我不知道爲什麼

example.js:26 Uncaught TypeError: Cannot read property 'length' of undefined 
    at HTMLAnchorElement.<anonymous> (example.js:26) 
    at HTMLElement.dispatch (jquery-3.2.1.min.js:1623) 
    at HTMLElement.q.handle (jquery-3.2.1.min.js:1585) 
(anonymous) @ example.js:26 
dispatch @ jquery-3.2.1.min.js:1623 
q.handle @ jquery-3.2.1.min.js:1585 

這裏是javascript部分。

$(function() { //when the DOM is ready 
    var times; //declare global variable 
    $.ajax({ //set up request 
     beforeSend: function (xhr) { //before requesting data 
      if (xhr.overrideMimeType) { //if supported 
       xhr.overrideMimeType("application/json"); // set MIME to prevent errors 
      } 
     } 
    }); 
    //funciton that collect data from the json file 
    function loadTimetable() { //decalre function 
     $.getJSON('data/example.json') //try to collect json data 
      .done(function (data) { //if succesful 
       times = data; //store in variable 
      }).fail(function() { //if a problem: show message 
       $('#event').html('Sorry! we couldnt load your time table at the moment'); 
      }); 
    } 
    loadTimetable(); //call the function 

    //CLICK ON TEH EVENT TO LOAD A TIME TABLE 
    $('#content').on('click', '#event a', function (e) { //user clicks on place 
     e.preventDefault(); //prevent loading page 
     var loc = this.id.toUpperCase(); //get value of id attr 
     var newContent = ""; 
     for (var i = 0; i < times[loc].length; i++) { // loop through sessions 
      newContent += '<li><span class = "time">' + times[loc][i].time + '</span>'; 
      newContent += '<a href = "descriptions.html#'; 
      newContent += times[loc][i].title.replace(/ /g, '-') + '">'; 
      newContent += times[loc][i].title + '</a></li>'; 
     } 
     $('#sessions').html('<ul>' + newContent + '</ul>'); // Display Time 
     $('#event a.current').removeClass('current'); // update selected link 
     $(this).addClass('current'); 
     $('#details').text(''); 
    }); 

    //CLICK ON A SESSION TO LEAD THE DESCRIPTION 
    $('#content').on('click', '#sessions li a', function (e) { //click on session 
     e.preventDefault(); // prevent loading 
     var fragment = this.href; //title is in href 
     fragment = fragment.replace('#', ' #'); //Add Space before # 
     $('#details').load(fragment); //to load info 
     $('#sessions a.current').removeClass('current'); //update selected 
    }); 

    //CLICK ON PRIMARY NAVIGATION 
    $('nav a').on('click', function (e) { //click on nav 
     e.preventDefault(); //prevent loading 
     var url = this.href; //get UR: to load 
     $('nav a.current').removeClass('current'); 
     $(this).addClass('current'); 
     $('#container').remove(); //remove old 
     $('#content').load(url + ' #container').hide().fadeIn('slow'); // add new 
    }); 
}); 

我覺得肯定有一些錯誤,我引用了其他文件在我的項目文件夾,因爲我工作過類似的項目,工作正常,但我就是想不通,我什麼辦法做錯了。

+0

它看起來像'times [loc]'是'undefined'。 –

+0

以下哪些行是第26行? –

+0

錯誤是說變量'times'沒有被定義(當你在for循環中引用它時)。你有沒有定義這個地方? –

回答

4

的關鍵,解決這個問題是閱讀,你沒有在你的標題發佈錯誤消息的部分:

無法讀取的不確定

這ISN財產「長度」 不要抱怨length財產本身。它告訴你times[loc]沒有定義。因此,您需要驗證times實際上是一個數組,並且loc的計算結果爲該數組的有效索引。

+0

事情是,它在我的代碼開始時被定義爲一個全局變量,現在我已將所有內容包含在我的帖子中。我認爲它沒有讀取我的.json文件,我不知道爲什麼。 – Adamwuh

+0

@Adamwuh那麼,你的變量不是全局的 - 它的作用域是'document.ready'函數。但是,在AJAX調用返回之前,您可能試圖訪問'times [loc]',在這種情況下,'times'變量尚未初始化。我會將整個'click'事件分配函數放到'done'回調函數中,以便您知道在設置之前您沒有使用'times'。另外,我會在'done'處理程序中記錄'times',以確保它獲得您期望的值。 –

相關問題