我有一个JSON问题,也许有人可以帮助我。我有以下JSON数组,我称之为“ showresults”:
[
{
"UniqueCode":"98917",
"NetworkUC":"c99af",
"Name":"Born This Way",
"ShowImage":"./assets/Born_This_Way_502x502.jpg",
"ShowLogo":"null"
},
{
"UniqueCode":"f583f",
"NetworkUC":"c99af",
"Name":"Bates Motel",
"ShowImage":"./assets/Bates_Motel_502x502.jpg",
"ShowLogo":"null"
},
{
"UniqueCode":"66ede",
"NetworkUC":"c99af",
"Name":"60 Days In",
"ShowImage":"./assets/60_Days_In_502x502.jpg",
"ShowLogo":"null"
},
{
"UniqueCode":"c3ecf",
"NetworkUC":"c99af",
"Name":"Intervention",
"ShowImage":"./assets/Intervention_502x502.jpg",
"ShowLogo":"null"
},
{
"UniqueCode":"0cac8",
"NetworkUC":"c99af",
"Name":"Wahlburgers",
"ShowImage":"./assets/Wahlburgers_502x502.jpg",
"ShowLogo":"null"
},
{
"UniqueCode":"a5866",
"NetworkUC":"c99af",
"Name":"Leah Remini: Scientology and the Aftermath",
"ShowImage":"./assets/Leah_remini_502x502.jpg",
"ShowLogo":"null"
},
{
"UniqueCode":"263f5",
"NetworkUC":"c99af",
"Name":"L.A. Burning: The Riots 25 Years Later",
"ShowImage":"./assets/LA_burning_502x502.jpg",
"ShowLogo":"null"
},
{
"UniqueCode":"804b6",
"NetworkUC":"c99af",
"Name":"Biggie: The Life of Notorious B.I.G.",
"ShowImage":"./assets/Biggie_502x502.jpg",
"ShowLogo":"null"
},
{
"UniqueCode":"6a9ce",
"NetworkUC":"c99af",
"Name":"The Murder of Laci Petersen",
"ShowImage":"./assets/Laci_paterson_502x502.jpg",
"ShowLogo":"null"
},
{
"UniqueCode":"b5513",
"NetworkUC":"c99af",
"Name":"Who Killed Tupac?",
"ShowImage":"./assets/Tupac_502x502.jpg",
"ShowLogo":"null"
},
{
"UniqueCode":"29b95",
"NetworkUC":"c99af",
"Name":"Life, Animated",
"ShowImage":"./assets/Life_animated_502x502.jpg",
"ShowLogo":"null"
},
{
"UniqueCode":"2d576",
"NetworkUC":"c99af",
"Name":"Live PD",
"ShowImage":"./assets/Livepd_502x502.jpg",
"ShowLogo":"null"
}
]
我创建了2个span标签,它们将显示“ Name”节点值。我看不到的问题是,如果页面当前正在显示showresults [3]中的内容,我希望一个span标签显示showresults [2] .Name的值,另一个希望显示showresults [4] 。名称。这是我的代码:
if (showresults.length > 0){
var first = showresults[0];
var count = showresults.length;
var toparrow = document.createElement("img");
toparrow.setAttribute("class", "top_arrow");
toparrow.setAttribute("src", "./images/top_arrow.jpg");
toparrow.setAttribute("uc", UniqueCode);
toparrow.addEventListener("click",LoadVideo);
var downarrow = document.createElement("img");
downarrow.setAttribute("class", "down_arrow");
downarrow.setAttribute("src", "./images/down_arrow.jpg");
downarrow.setAttribute("uc", UniqueCode);
downarrow.addEventListener("click", LoadVideo);
var backShow = document.createElement("span");
backShow.setAttribute("class","back");
backShow.setAttribute("uc", UniqueCode);
backShow.addEventListener("click", LoadVideo);
var forwardShow = document.createElement("span");
forwardShow.setAttribute("class","forward");
forwardShow.addEventListener("click", LoadVideo);
for (var i = 0; i < showresults.length; i++) {
if (i == 0){
var curobj = showresults[i];
var curobjnext = showresults[i++];
var curobjprevious = showresults[i--];
var UniqueCode = curobj["UniqueCode"];
var Name = curobj["Name"];
var NameNext = curobjnext["Name"];
var NamePrevious = curobjprevious[Name];
backShow.innerHTML = NamePrevious;
forwardShow.innerHTML = NameNext;
if(curobj == first){
$('#video').append(downarrow);
$('#video').append(forwardShow);
} else {
$('#video').append(toparrow);
$('#video').append(downarrow);
$('#video').append(backShow);
$('#video').append(forwardShow);
}
}
}
}
“ NameNext”和“ NamePrevious”变量未返回预期结果。有人可以指出正确的方向吗?
最佳答案
更改为此:
if (showresults.length > 0){
var first = showresults[0];
var count = showresults.length;
var toparrow = document.createElement("img");
toparrow.setAttribute("class", "top_arrow");
toparrow.setAttribute("src", "./images/top_arrow.jpg");
toparrow.setAttribute("uc", UniqueCode);
toparrow.addEventListener("click",LoadVideo);
var downarrow = document.createElement("img");
downarrow.setAttribute("class", "down_arrow");
downarrow.setAttribute("src", "./images/down_arrow.jpg");
downarrow.setAttribute("uc", UniqueCode);
downarrow.addEventListener("click", LoadVideo);
var backShow = document.createElement("span");
backShow.setAttribute("class","back");
backShow.setAttribute("uc", UniqueCode);
backShow.addEventListener("click", LoadVideo);
var forwardShow = document.createElement("span");
forwardShow.setAttribute("class","forward");
forwardShow.addEventListener("click", LoadVideo);
for (var i = 0; i < showresults.length; i++) {
if (i == 0){
var curobj = showresults[i];
var curobjnext = showresults[i+1]; // change here
var curobjprevious = showresults[i-1]; // and here
var UniqueCode = curobj["UniqueCode"];
var Name = curobj["Name"];
var NameNext = curobjnext["Name"];
var NamePrevious = curobjprevious[Name];
backShow.innerHTML = NamePrevious;
forwardShow.innerHTML = NameNext;
if(curobj == first){
$('#video').append(downarrow);
$('#video').append(forwardShow);
} else {
$('#video').append(toparrow);
$('#video').append(downarrow);
$('#video').append(backShow);
$('#video').append(forwardShow);
}
}
}
}
但是我没有得到的是,您仅通过以下方式显示了第一项:
if (i == 0){
这意味着在showresults[i-1]
i
中将为-1 ...因此,通过showresults[showresults.length]
变为无限或不显示以前的结果...