我有下一个js代码。

var myData=[];
for(j=1;j<=Pages;j++)
{
$.ajax({
        type: 'get',
        url: 'link.xml'+j,
        async: false,
        dataType: 'xml',
        success: function(data){
            getData(data);
        },
        error: function(data){
            console.log("error");
        }
    });
}
function getData(data){


     var tmpData = {id:'' , displayName:''};

     //taking the values and storing them to myData


            myData.push(tmpData);
            tmpData = {id:'' , displayName:'', eventsHref: []};


}


    for(i=0;i<myData.length;i++)
    {
     $.ajax({
        type: 'get',
        url: 'somelink'+data[i].id,
        async: false,
        dataType: 'xml',
        success: function(events){
            getUpEvents(events);
        },
        error: function(events){
            console.log("error");
        }
    });
    }
    function getUpEvents(events){

    var tmpEvents = {displayNameEvent:[] , displayNameArtist: []};

     //taking some other values and storing them to myData

     myData.push(tmpEvents);
     tmpEvents = {displayNameEvent:[] , displayNameArtist:[]};

}


现在以特定方式打印myData的结果。在第一行myData [0] .displayName。接下来的行中,所有指示myData [0] .displayName和所有myData [i]的myData [i] .displayNameEvents .displayNameArtist。之后,它将打印下一个myData [1] .displayName,依此类推。

以下是我尝试打印它们的方式。

for(i=0;i<myData.length;i++)
{
        document.write(myData[i].displayName+"<br>");
        document.write(myData[i].displayNameEvent+"<br>");
        document.write(myData[i].displayNameArtist+"<br>");
}

最佳答案

//Create a div to hold the results
var results = $("<div/>");

//Loop through the data and append each value wrapped in a p to the div
for(i=0;i<myData.length;i++)
{
        results.append("<p>" + myData[i].displayName + "</p>");
        results.append("<p>" + myData[i].displayNameEvent + "</p>");
        results.append("<p>" + myData[i].displayNameArtist + "</p>");
}

//append the div to the body
$("body").append(results);

关于javascript - 如何使用javascript/jquery打印数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20224114/

10-13 06:52