我正在使用此代码更改图像 src,但它不起作用。如果我为变量文本值写狐狸示例:
track = '212';
artist = 'azealea banks';
它只改变最后一个图像 src 但是当我从邻居跨度获取这个变量值时它根本不起作用。
我的 jQuery 代码:
$(function () {
$(".plimg").attr("src",
function (index) {
var title = $(this).next('span.titletrack').text();
alert(title);
var array = title.split(' - ');
var track = array[0];
var artist = array[1];
var output;
$.ajax({
url: "http://ws.audioscrobbler.com/2.0/?method=track.search",
data: {
track: track,
artist: artist,
api_key: "ca86a16ce762065a423e20381ccfcdf0",
format: "json",
lang: "en",
limit: 1
},
async: false,
success: function (data) {
output = data.results.trackmatches.track.image[0]["#text"];
}
});
return output;
});
});
和 HTML
<div id="playlist" class="scrollable" style="height: 300px;overflow-y: auto">
<li>
<img src="/img/playlist/33a - Sulis Vardo.jpg">
<span class="titletrack">33a - Sulis Vardo</span>
</li>
<li>
<img class="plimg" src="/img/playlist/33a - Shota.jpg">
<span onclick="playinToplaylist($(this).html());" class="titletrack">33a - Shota</span>
</li>
</div>