0
我正在下頁:對象的JavaScript數組在頁面加載過程中返回零長度,但不在控制檯中返回?
問題代碼(這是內聯,可以在頁面的848行找到):
<script>
var listType = "Basic",
theList92 = document.querySelector('#block-92 > ol'),
finalEventsList92 = "",
futureEvents92 = [],
pastEvents92 = [],
nonWiley92 = [],
events92 = [{
title: "Golf Benefit 2015",
date: new Date("2015, 7, 20"),
time: "10:30 am",
series: "Friends of the Waisman Center",
speaker: "",
thumbnail: "/images/Golf/GolfPhoto75x100.jpg",
location: "Bishops Bay Country Club", url: "/events2015-GolfBenefit.htm"
},{
title: "David Stokes",
date: new Date("2015, 5, 10"),
time: "1 pm",
series: "Children's Theatre",
speaker: "David Stokes",
thumbnail: "/images/CT/StokesDavidPuppets75x100.jpg",
location: "Friends of the Waisman Center Auditorium", url: "/events-ctS2015-Stokes.htm"
},{
title: ""The Genetics of Cerebellar Development and Function"",
date: new Date("2015, 4, 24"),
time: "Noon",
series: "John D. Wiley Seminar Series",
speaker: "Dan Goldowitz, PhD",
thumbnail: "/images/Seminars/GoldowitzDan75x100.jpg",
location: "John D. Wiley Conference Center", url: "/seminars-2015-Apr24-Goldowitz.htm"
},{
title: ""The Infant Brain Imaging Study (IBIS): Insights into the Early Development of Autism"",
date: new Date("2015, 4, 17"),
time: "Noon",
series: "John D. Wiley Seminar Series",
speaker: "Joseph Piven, MD",
thumbnail: "/images/Seminars/PivenJoe75x100.jpg",
location: "John D. Wiley Conference Center", url: "/seminars-2015-Apr17-Piven.htm"
},{
title: "Snow White",
date: new Date("2015, 4, 12"),
time: "1 pm",
series: "Children's Theatre",
speaker: "PlayTime Productions",
thumbnail: "/images/CT/PlaytimeBanner75x100.jpg",
location: "Friends of the Waisman Center Auditorium", url: "/events-ctS2015-SnowWhite.htm"
},{
title: ""Synaptic and Circuitry Mechanisms of Psychiatric Disorders"",
date: new Date("2015, 4, 10"),
time: "Noon",
series: "John D. Wiley Seminar Series",
speaker: "Guoping Feng, PhD",
thumbnail: "/images/Seminars/FengGuoping75x100.jpg",
location: "John D. Wiley Conference Center", url: "/seminars-2015-Apr10-Feng.htm"
},{}];
events92.pop();
if(events92.length === 0){
finalEventsList92 = "<li>No matching events listed at this time.</li>";
} else {
if("Yes" === "Yes"){
events92.forEach(function(wcEvent, index, array){
if(wcEvent.series !== "John D. Wiley Seminar Series"){
nonWiley92.push(wcEvent);
}
});
events92 = nonWiley92.reverse();
}
if("Future" === "Future"){
events92.forEach(function(wcEvent, index, array){
if(wcEvent.date >= Date.now()){
futureEvents92.push(wcEvent);
}
});
futureEvents92.reverse();
if(futureEvents92.length === 0){
finalEventsList92 = "<li>No matching events listed at this time.</li>";
} else {
futureEvents92.reverse();
for(var i = 0; i < 6; i++){
if(futureEvents92[i]){
if(listType === "Basic"){
finalEventsList92 += buildBasicListItem(futureEvents92[i]);
} else {
finalEventsList92 += buildListItemWithImage(futureEvents92[i]);
}
}
}
}
}
if("Future" === "Past"){
events92.forEach(function(wcEvent, index, array){
if(wcEvent.date < Date.now()){
pastEvents92.push(wcEvent);
}
});
if(pastEvents92.length === 0){
finalEventsList92 = "<li>No matching events listed at this time.</li>";
} else {
pastEvents92.reverse();
for(var i = 0; i < 6; i++){
if(pastEvents92[i]){
if(listType === "Basic"){
finalEventsList92 += buildBasicListItem(pastEvents92[i]);
} else {
finalEventsList92 += buildListItemWithImage(pastEvents92[i]);
}
}
}
}
}
if("Future" === "All"){
for(var i = 0; i < 6; i++){
if(events92[i]){
if(listType === "Basic"){
finalEventsList92 += buildBasicListItem(events92[i]);
} else {
finalEventsList92 += buildListItemWithImage(events92[i]);
}
}
}
}
}
theList92.innerHTML = finalEventsList92;
</script>
在所有瀏覽器都在那裏初始評估events92.length === 0
,在控制檯中,運行相同的命令實際上給出了正確的長度(在這種情況下爲66)。此外,我知道有一些奇怪的比較(如"Future" === "Future"
,但列表是由CMS生成的,這是我們如何讓用戶調整列表的顯示,因爲CMS不像WordPress那樣動態。
任何幫助表示讚賞
你等到頁面完成渲染你算對象之前? – 2014-09-18 15:40:56
我猜你試圖在DOM完全加載之前獲取對象,這將返回0.length的長度。 – 2014-09-18 15:41:27
@PaulTomblin在創建對象後立即計算對象。如果您使用檢查器,則可以看到正在創建集合的腳本標記。我會將代碼添加到原始帖子中。 – jkinz 2014-09-18 16:58:47