作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 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 个跨度标签,它们将显示“名称”节点值。我无法弄清楚的问题是页面当前是否显示 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]
无限,要么不显示之前的结果...
关于javascript - 如何从下一个json数组节点获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46109382/
我是一名优秀的程序员,十分优秀!