我是編程新手。我正在嘗試使用JQuery將JSON數據顯示到HTML文件中。我在控制檯中出現了Uncaught TypeError: Cannot read property 'length' of undefinedresumeBuilder.js:147 education.displayresumeBuilder.js:178 (anonymous function)
的錯誤,我不知道爲什麼。任何你可以解決這個錯誤的燈都會非常感謝!JSON JQUERY未捕獲TypeError:無法讀取未定義的屬性「長度」
這裏是我的resumeBuilder.js文件:
var eduction = {
"schools": [
{
"name": "McGill University",
"location": "Montreal, Quebec, Canada",
"degree": "Master of Arts",
"major": "Second Language Education",
"url": "mcgill.ca"
}
],
"onlineCourses": [
{
"title": "Full Stack Web Development Course",
"school": "Bloc.io",
"url": "bloc.io"
}
]
};
education.display = function() {
if(education.schools.length > 0 || education.onlineCourses.length > 0) {
for(i in education.schools) {
$("#education").append(HTMLschoolStart);
var formattedSchoolName = HTMLschoolName.replace("%data%", education.schools[i].name).replace("#", education.schools[i].url);
var formattedSchoolDegree = HTMLschoolDegree.replace("%data%", education.schools[i].degree);
var formattedSchoolLocation = HTMLschoolLocation.replace("%data%", education.schools[i].location);
var formattedSchoolMajor = HTMLschoolMajor.replace("%data%", education.schools[i].major);
$(".education-entry:last").append(formattedSchoolName + formattedSchoolDegree);
$(".education-entry:last").append(formattedSchoolLocation);
$(".education-entry:last").append(formattedSchoolMajor);
}
if(education.onlineCourses.length > 0) {
$("#education").append(HTMLonlineClasses);
for(i in education.onlineCourses) {
$("#education").append(HTMLschoolStart);
var formattedOnlineTitle = HTMLonlineTitle.replace("%data%", education.onlineCourses[i].title).replace("#", education.onlineCourses[i].url);
var formattedOnlineSchool = HTMLonlineSchool.replace("%data%", education.onlineCourses[i].school);
var formattedOnlineURL = HTMLonlineURL.replace("%data%", education.onlineCourses[i].url).replace("#", education.onlineCourses[i].url);
$(".education-entry:last").append(formattedOnlineTitle);
$(".education-entry:last").append(formattedOnlineSchool);
$(".education-entry:last").append(formattedOnlineURL);
}
}
}
}
education.display();
這裏是我的helper.js文件:
var HTMLschoolStart = '<div class="education-entry"></div>';
var HTMLschoolName = '<a href="#">%data%';
var HTMLschoolDegree = ' -- %data%</a>';
var HTMLschoolLocation = '<div class="location-text">%data%</div>';
var HTMLschoolMajor = '<em><br>Major: %data%</em>';
var HTMLonlineClasses = '<h3>Online Classes</h3>';
var HTMLonlineTitle = '<a href="#">%data%';
var HTMLonlineSchool = ' - %data%</a>';
var HTMLonlineURL = '<br><a href="#">%data%</a>';
你能告訴我們在這個代碼行是':178'? – void 2015-02-23 22:18:34