无法更改音频源

时间:2017-04-18 01:39:22

标签: javascript jquery audio

我见过其他主题,但他们的代码非常复杂。我希望地雷很简单,所以我很容易理解它。我觉得这很容易,但事实证明这很难。显然,您无法通过更改其属性来更改音频源。我希望在复选框发生变化时更改音频源,然后它将变为相应的音频文件。

HTML

<select id="ccw" onblur="check()">
<option>Lobby</option>
<option>Autumn</option>
<option>Winter</option>
<option>Spring</option>
<option>Summer</option>
</select>
<audio class="stage10" controls>
<source src="default.mp3" type="audio/mpeg">
Audio element not supported by your browser
</audio>

JS

var songs = [
//lobby
"default.mp3",
//autumn
"fall.mp3",
//winter
"the ice age.mp3",
//spring
"AHHHH ITS BEEEEEEEES.mp3",
//summer
"Da Summa of 1989 or was it 1998 i dont know.mp3"

//please do not take notes of the file's names
]

$(function() {
    $('#ccw').on('change', check)
})
function check() {
    var ccw = $('#ccw').val()
    if (ccw == "Autumn") {
        $('#stage10 source').attr('src', songs[1])
    } else if (ccw == "Winter") {
        $('#stage10 source').attr('src', songs[2])
    } else if (ccw == "Spring") {
        $('#stage10 source').attr('src', songs[3])
    } else if (ccw == "Summer") {
        $('#stage10 source').attr('src', songs[4])
    } else {
        $('#stage10 source').attr('src', songs[0])
    }
    $('audio')[10].load() // this wasn't here before, but i tried testing with it.
}

我不确定为什么它不起作用。有人请帮助我!

1 个答案:

答案 0 :(得分:0)

&#34;#stage10&#34;是一个ID选择器,用&#34; .stage10&#34;替换它。如果你想逐个选择元素:

function check() {
    var ccw = $('#ccw').val()
    if (ccw == "Autumn") {
        $('.stage10 source').attr('src', songs[1])
    } else if (ccw == "Winter") {
        $('.stage10 source').attr('src', songs[2])
    } else if (ccw == "Spring") {
        $('.stage10 source').attr('src', songs[3])
    } else if (ccw == "Summer") {
        $('.stage10 source').attr('src', songs[4])
    } else {
        $('.stage10 source').attr('src', songs[0])
    }
    $('audio')[10].load() // this wasn't here before, but i tried testing with it.
}