在前端,有一個選擇框和音樂播放器。如何使用JQuery播放連續歌曲?
這裏是我的HTML,以幫助從齊射Nostrato:
<!DOCTYPE html>
<html>
<head>
<title> Test </title>
<script src="jquery-3.2.1.min.js"> </script>
<script src="whatToPlay.js"> </script>
</head>
<body onload="onload();">
<select id="changeselection" name="change-selection">
<option id="change1" value="change1" selected>Song 1</option>
<option id="change2" value="change2">Song 2</option>
<option id="change3" value="change3">Song 3</option>
</select>
<audio id="audio1" tabindex="0" controls="" type="audio/mpeg" controls preload>
<source id="audiochange" type="audio/mp3" src="audio/default.mp3">
Sorry, your browser does not support HTML5 audio.
</audio>
</body>
</html>
和我whatToPlay.js文件,以幫助從齊射Nostrato(jQuery的):
$(document).ready(function() {
$("#changeselection").on("change",function(){
audio=$("#audio1");
$("#audio1").attr("src",src);
if($(this).val()=="change2"){
var src = "audio/song2.mp3";
console.log('change2');
}
else if ($(this).val()=="change3"){
var src = "audio/song3.mp3";
console.log('change3');
}
else {
var src = "audio/default.mp3";
console.log('change1');
}
audio=$("#audio1");
$("#audio1").attr("src",src);
audio[0].pause();
audio[0].load();//suspends and restores all audio element
audio[0].play();
});
});
當我選擇歌曲2(id =「chang e2「),我想播放音頻/ song2.mp3,然後在播放結束後立即播放audio/song2A.mp3。我如何最低限度地將此添加到我的代碼?
查找['onend'事件](https://www.w3schools.com/tags/av_event_ended.asp) –
@LouysPatriceBessette但我應該在哪裏添加這在我的jQuery的文件,如果我想音頻/ song2A .mp3在選擇了歌曲2(id =「change2」)時是否只有audio/song2.mp3?我不希望它影響任何其他選項。 – user604803