下次单击Json + JS + Jquery时显示上一项

时间:2014-03-09 14:24:16

标签: javascript jquery json

我有这个功能来显示博客条目:

 function getPrev(item, articles) {
   for (var i = 0, len = articles.length; i < len; i++) {
      if (articles[i].id === item) {
          return (i - 1 >= 0) ? articles[i - 1] : null;
      }
   }
   return null;
 }

这一个:

  function getNext(item, articles) {
     for (var i = 0, len = articles.length; i < len; i++) {
        if (articles[i].id === item) {
          return (typeof (articles[i + 1]) !== 'undefined') ? articles[i + 1] : null;
        }
     }
     return null;
   }

最后是ajax电话:

  function getArticle(which) {
$.ajax({
    type: "GET",
    url: "../Content/test.txt",
    dataType: "json",
    success: function (data) {
        var entry;
        var id = parseInt(document.getElementById('blogcont').getAttribute('data-id')) || 0;

        if (which === 'next') {
            entry = getNext(id, data);
        } else if (which === 'prev') {
            entry = getPrev(id, data);
        } else {
            entry = data[0];
        }

        if (entry) {
            document.querySelector('#blogcont .headline').innerHTML = entry.Title;
            document.querySelector('#blogcont .content').innerHTML = entry.Content;
            document.getElementById('blogcont').setAttribute('data-id', entry.id);
        }
    },
    error: function (data) {
        console.error('Error in AJAX call');
        console.error(data);
    }
});
}

我希望在显示当前时显示左侧面板中的前一个(类似)。有什么想法吗?我的HTML:                                                               

                

            <button class="nvgt" id="prev">Previous</button>            
            <button class="nvgt" id="next">Next</button>
        </div>
        <div id="articles">
            <div id="similar" class="box"></div>
            <div id="popular" class="box"></div>
            <div id="thisblog" class="box"></div>
        </div>

由于

0 个答案:

没有答案
相关问题