这是我的代码:
$.getJSON('https://api.syfaro.net/minecraft/1.2/server/info?ip=76.171.110.176&port=25565', function (data) {
document.getElementById('lastUpdate').textContent = "Last Update: " + data.last_update + "(GMT-6)";
document.getElementById('version').textContent = "Minecraft Version: MC Release V" + data.version;
document.getElementById('serverVersion').textContent = "Server Version: Last Update " + data.motd;
if (data.status == "success") {
document.getElementById('online').textContent = "Server is online!";
if (data.players === 0) {
document.getElementById('playerListHTML').textContent = "No players are currently online.";
} else {
for (var i = 0; i < data.playerList.length; i++) {
printList.push(data.playerList[i] + ".png");
var img = document.createElement("img" + [i]);
img.setAttribute("id", "image" + [i]);
document.getElementById("image" + [i]).src = "http://signaturecraft.us/avatars/5/face/" + data.playerList[i] + ".png";
}
//document.getElementById('playerListHTML').textContent = printList;
}
} else {
document.getElementById('online').textContent = "Server is not online... try again later.";
document.getElementById('playersListHTML').textContent = "Server is not online... try again later.";
}
});
基本上,我想从一个网站的API中获取一个JSON,它将在线播放器列表作为一个数组,在线为每个玩家创建一个元素,然后将src(图像源)设置为播放器&# 39岁的脸。 以下是我在线时所需的src网址示例:http://signaturecraft.us/avatars/5/face/BtheDestroyer.png
如果有人能弄清楚如何让这个愚蠢的事情发挥作用,我真的很感激你的帮助!
答案 0 :(得分:0)
由于您正在使用jQuery转换所有构造以使用jQuery语法。
我可以看到的一个问题是创建的图像元素没有添加到dom结构中,请尝试
$.getJSON('https://api.syfaro.net/minecraft/1.2/server/info?ip=76.171.110.176&port=25565', function (data) {
$('#lastUpdate').html("Last Update: " + data.last_update + "(GMT-6)")
$('#version').html("Minecraft Version: MC Release V" + data.version)
$('#serverVersion').html("Server Version: Last Update " + data.motd)
if (data.status == "success") {
$('#lastUpdate').html()
if (data.players === 0) {
$('#playerListHTML').html("No players are currently online.")
} else {
$('#playerListHTML').empty()
$.each(data.playerList, function (idx, player) {
$('<img/>', {
id: 'image[' + idx + ']',
src: "http://signaturecraft.us/avatars/5/face/" + player + ".png"
}).appendTo('#playerListHTML')
})
}
} else {
$('#online').html("Server is not online... try again later.")
$('#playersListHTML').html("Server is not online... try again later.")
}
});
演示:Fiddle