1

I'm making an audio player script that changes the src="" of the <audio> tag, I don't like doing because I'm not doing tracks.

The user clicks on this link:

<a href="#play" onclick="playAudio('song.mp3');">Play</a>

Then this receives the function:

   function playAudio(file)
    {
    var audio = file;
    }
    if(audio !== '') {
    document.getElementById('player').src = 'music/'+audio;
    }
    else {

    }

For this player:

<audio id="player" src="" controls></audio>

I've tried changing different things about the script but it's no good.

Why won't it change the audio?

4

1 回答 1

3

您的函数在开始时结束,因为您的右大括号}在错误的位置:

function playAudio(file)
    {
    var audio = file;
    }

这应该写成:

 function playAudio(file)
    {
    var audio = file;
    if(audio !== '') {
    document.getElementById('player').src = 'music/'+audio;
    }
    else {

    }
}

另外,id不是一个href,这应该是:

<a href="#play" id="play" onclick="playAudio('song.mp3');">Play</a>
于 2013-09-02T21:31:01.170 回答