我試圖製作一個菜單,其中包含從last.fm API中提取的藝術家名稱和圖像。目前,它生成的圖像和名稱罰款清單,除了在列表頂部打印「未定義」。我試着用藝術家名字/圖像來評論元素,並且只是讓它生成元素,但它仍然會打印未定義的元素。我完全難以理解它所說的未定義。Javascript生成的HTML也打印「undefined」
首先,它使一個AJAX調用來獲取所需的信息:它處理信息,並生成它與inner.HTML
var artistsAndImage = {};
for (var i=0; i < artistList["artists"]["artist"].length; i++) {
var name = artistList["artists"]["artist"][i]["name"];
artistsAndImage[name] = artistList["artists"]["artist"][i]["image"][0]["#text"];
};
var code;
// Generate HTML for sidebar with artist names and iamges
for (var name in artistsAndImage) {
nameid = name.
replace(/ /g, "_").
replace(/&/g, ":amp").
replace(/\+/g, ":plus");
code += "<element id=\"" + nameid + "\" onclick=\"details(this)\">\n<p style = \"cursor:default\">\n <img src=\"" + artistsAndImage[name] + "\" alt=\"alt\" width = \"50\" height = \"50\">\n" + name + "\n</p> </element>\n";
};
document.getElementById("sidebar").innerHTML = "<div style = \"padding:20px\">" + code + "</div>";
實現HTML哪裏「未定義
if (window.XMLHttpRequest) { // Non-IE browsers
httpRequest = new XMLHttpRequest();
}
else if (window.ActiveXObject) { // IE 8 and older
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
httpRequest.open("GET", 'http://ws.audioscrobbler.com/2.0/?method=chart.gettopartists&api_key=b25b959554ed76058ac220b7b2e0a026&format=json', true);
httpRequest.onreadystatechange = function() {
var done = 4, ok = 200;
// Parse the JSON into JavaScript immediately on receipt
if (httpRequest.readyState == done && httpRequest.status == ok) {
artistList = JSON.parse(httpRequest.responseText);
然後「來自,我該如何解決?你可以看到頁面的當前版本上傳這裏:http://test.yukot.corp.he.net/
可能重複[什麼導致了這種額外的 「不確定」?](http://stackoverflow.com/questions/7381618/what- IS-造成此結果超未定義) –