翻阅记录使用jQueryjQuery

2023-09-10 13:24:37 作者:点击查看大图

我有一个JSON的结果,其中包含许多纪录。我想显示第一个,但下一个按钮,查看第二个,依此类推。我不想页面刷新这就是为什么我希望JavaScript的,jQuery的组合,甚至是第三方AJAX库可以提供帮助。

I have a JSON result that contains numerous records. I'd like to show the first one, but have a next button to view the second, and so on. I don't want the page to refresh which is why I'm hoping a combination of JavaScript, jQuery, and even a third party AJAX library can help.

有什么建议?

推荐答案

希望这有助于:

var noName = {
    data: null
    ,currentIndex : 0
    ,init: function(data) {
        this.data = data;
        this.show(this.data.length - 1); // show last
    }
    ,show: function(index) {
        var jsonObj = this.data[index];
        if(!jsonObj) {
            alert("No more data");
            return;
        }
        this.currentIndex = index;
        var title = jsonObj.title;
        var text = jsonObj.text;
        var next = $("<a>").attr("href","#").click(this.nextHandler).text("next");
        var previous = $("<a>").attr("href","#").click(this.previousHandler).text("previous");

        $("body").html("<h2>"+title+"</h2><p>"+text+"</p>");
        $("body").append(previous);
        $("body").append(document.createTextNode(" "));
        $("body").append(next);
    }
    ,nextHandler: function() {
        noName.show(noName.currentIndex + 1);
    }
    ,previousHandler: function() {
        noName.show(noName.currentIndex - 1);
    }
};

window.onload = function() {
    var data = [
        {"title": "Hello there", "text": "Some text"},
        {"title": "Another title", "text": "Other"}
    ];
    noName.init(data);
};