jQuery在点击时迭代数组

时间:2012-06-15 04:59:44

标签: javascript jquery xml ajax

我试图到处寻找解决方案,但我似乎无法理解如何正确完成这项工作。基本上我有一个XML,我想从中提取数据。我希望页面加载第一个条目,一旦用户单击前进按钮,我希望它迭代XML直到它结束。后退按钮也是如此。

    $(document).ready(function(){
        var name = [];
        var description = [];
        var html_screenshot_01 = [];
        var html_screenshot_02 = [];
        var site_link = [];
        var source_link = [];
        var demo_link = [];

        $.ajax({
            type: "GET",
            url: "projects_html.xml",
            dataType: "xml",
            success: function(xml) {
                $(xml).find('name').each(function(){ name.push($(this).text()); });
                $(xml).find('description').each(function(){ description.push($(this).text()); });
                $(xml).find('html_screenshot_01').each(function(){ html_screenshot_01.push($(this).text()); });
                $(xml).find('html_screenshot_02').each(function(){ html_screenshot_02.push($(this).text()); });
                $(xml).find('site_link').each(function(){ site_link.push($(this).text()); });
                $(xml).find('source_link').each(function(){ source_link.push($(this).text()); });
                $(xml).find('demo_link').each(function(){ demo_link.push($(this).text()); });                   

                $(xml).find('projects').each(function(){


                    $('#name').empty().append(name[0]);
                    $('#description').empty().append(description);
                    $("img#html_screenshot_01").attr("src", html_screenshot_01);
                    $("img#html_screenshot_02").attr("src", html_screenshot_02);
                    $("a#site_link").attr("src", site_link);
                    $("a#source_link").attr("src", source_link);
                    $("a#demo_link").attr("src", demo_link);    

                    $('a#site_link').filter(function() {return $.trim($(this).text()) === ''}).css("visibility", "hidden")
                    $('a#source_link').filter(function() {return $.trim($(this).text()) === ''}).css("visibility", "hidden")                    
                    $('a#demo_link').filter(function() {return $.trim($(this).text()) === ''}).css("visibility", "hidden")
                });
            }
        });
        $("#next").click(function() {
            var i = 0;
            i++;
            $('#name').empty().append(name[i]);
            return false;
        });
        $("#back").click(function() {
            alert('back');
        });
    });

1 个答案:

答案 0 :(得分:0)

var i = 0;移出下一个点击事件

.
.

var i = 0;
$("#next").click(function() {
.
.
.