我有一個系列的每個<div>
s的顏色名稱,如<div id="white"></div>
加載的音頻文件,並填寫了幾個標題標籤與藝術家和曲目標題來自一個Ajax調用聚集在div點擊我可以更改循環中的索引嗎?
時的ID HTML
<audio id="song" preload="none">
</audio>
<div id="white"></div>
<div id="pink"></div>
<div id="play" onclick="document.getElementById('song').play()"></div>
<div>
<h2 id="title"></h2>
<h3 id="artist"></h3>
</div>`
的Javascript
$("#white").click(function(){
$("#song").attr('src',data[0].songSrc);
$("h2").html(data[0].title)
$("h3").html(data[0].artist)
});
$("#pink").click(function(){
$("#song").attr('src',data[1].songSrc);
$("h2").html(data[1].title)
$("h3").html(data[1].artist)
});
我可以使用一個for循環或$.each
而不是重複相同的代碼和手動更改DIV這19個項目的每一個的ID和項目索引?
這裏是我的bin其中,我的工作了這一點:jsbin
相關HTML:
<div id="play" onclick="document.getElementById('song').play()"></div>
我猜你是從根本上就錯了! ':(' –
@Praveen,你可以更具體嗎? –
這就是我仍然試圖理解的。':(' –