简体   繁体   English

如何显示数组的下一个/上一个项目?

[英]How to Show Next/Previous item of an array?

I'm writing the first item of an array to the screen, and would like to create Next/Previous buttons for array, but I can't get it to work. 我正在将数组的第一项写入屏幕,并想为数组创建“下一个/上一个”按钮,但无法正常工作。 I have tried several methods, but I can't find suitable solution. 我尝试了几种方法,但是找不到合适的解决方案。

Can anyone help? 有人可以帮忙吗?

This is the last one I have tried: 这是我尝试的最后一个:

        var data = [
            {"subject":"starcraft2",
             "date":"08.31",
             "dDay":"mon",
             "content1":"STARCRAFT2",
             "content2":"season2",
             "playerA":"Ju",
             "playerB":"Lee",
             "emblemA":"Terran",
             "emblemB":"Zerg",
             "result":"end"},
            {"subject":"starcraft2",
             "date":"08.29",
             "dDay":"wed",
             "content1":"STARCRAFT2",
             "content2":"season2",
             "playerA":"kim",
             "playerB":"joo",
             "emblemA":"Terran",
             "emblemB":"Protoss",
             "result":"end"},
         ];

         function prevAction() {
             // function (e) { // the e here is the event itself
             alert("Prev Click!");
                 // document.getElementById('subject').textContent = prevItem();
                 // document.getElementById('date').textContent = prevItem();
                 for (var i = 0; i<data.length; i++)
                     for (var j=0; j<data[i]; j++)
                         while(j === 0)
                         {
                            j == j++;
                            console.log(j);
                         }
                     console.log(data[j].date + ', ');
                     document.getElementById('date').textContent = data[j].date;
                     // document.getElementById('subject').textContent = j[0];
                 }

Here's the jist of how you'd accomplish this in pure Javascript: 这是您如何用纯Javascript完成此工作的要旨:

getNextItem() {
  var index = document.getElementById("index").value;
  //add guards here to prevent array overflow/underflow
  if (data.length < index - 1) {
      index.value++; 
  }
  document.getElementById("DOM_ELEMENT_TO_ATTACH_DATA").innerHTML = data[index];
}

getPreviousItem() {
  var index = document.getElementById("index").value;
  //add guards here to prevent array overflow/underflow
  if (index > 0) {
    index.value--;
  } 
  document.getElementById("DOM_ELEMENT_TO_ATTACH_DATA").innerHTML = data[index];
}


<input id="index" type="hidden" name="index" value="0">
<button type="button" onclick="getNextItem()">Next Item</button>
<button type="button" onclick="getPreviousItem()">Previous Item</button>

Note that this will just attach the pure json data to a DOM element, it won't do anything fancy with it. 请注意,这只会将纯json数据附加到DOM元素,它不会做任何花哨的事情。

Furthermore, if you want to do anything more complex, I'd strongly recommend you look into using a library like jQuery or Angular as it's going to make your life a whole lot easier in the long run. 此外,如果您想做更复杂的事情,强烈建议您考虑使用jQueryAngular之类的库,因为从长远来看,它将使您的生活变得更加轻松。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM