目前我已经在我的网站www.midnightlisteners.com上集成了 Last.fm API,但它会将所有 Last.fm 数据放在最后一个 Kanye West 上。如果您将鼠标悬停在 (i) 图标上,您将看到数据出现在工具提示中。
我想遍历所有并将它们添加到相应的位置。另外,如果有人也可以帮助我获得小型艺术家的图像,那就太好了。
我的jQuery代码:
$(document).ready(function() {
// Find Related Artists based on Last.fm JSON Results
$(".artist-data").each(function() {
// Find the artist name in the "p" tag and save it
artistName = $(this).find(".artist-wrap-mid p");
artist = artistName.text();
// Create a class out of the artist name made of lowercase letters and "-" instead of spaces
artistClass = artist.toLowerCase().replace(/ /g, '-');
// Add this class to the .artist-data div
$(this).addClass(artistClass);
// Check if a value is present
if (artist === '') {
$("." + artistClass + " .related").html("No related artist info found for " + artist);
}
// Otherwise return the request with links to each related artist
else {
$.getJSON("http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist=" + artist + "&api_key=9c991c10bf461ac4e4f92fdfa14c20c2&limit=3&format=json&callback=?", function(data) {
var html = '';
$.each(data.similarartists.artist, function(i, item) {
html += "<a href='http://" + item.url + "' target='_blank'>" + item.name + "</a>, ";
}); // End each
$("." + artistClass + " .related").append(html);
}); // End getJSON
} // End Else
});
});
我的 HTML 最好在我的网站上看到:www.midnightlisteners.com
但它将 Last.fm 中的所有数据放入<div class="related"> </div>
我在这里得到了很多帮助:writing.sackettsolutions.com/2012/02/navigating-the-last-fm-api-with-a-little-help-from-jquery-getjson