0

我正在尝试使用 JavaScript 构建一个简单的 HTML5 音乐播放器。我用这段代码构建了一个播放器:

<audio controls="controls">
    <source id="song" src="horse.ogg" type="audio/ogg">
</audio>

我已经构建了几个<a>调用函数 changeSong(n);

function changeSong(n) {
    document.getElementById("song").src = "song" + n + ".mp3";
}

当我单击<a>' 时,文档没有任何反应,并且它不起作用..

<a>

<a href="#1" onclick="changeSong(1)">Play song 1 </a></br>
<a href="#2" onclick="changeSong(2)">Play song 2 </a>

怎么做才能让它发挥作用?////

我尝试只运行不带函数的 JavaScript,它可以工作。但是当它从作为函数调用时它不会......

4

4 回答 4

1

改变:

<a href="#2" onclick="playSong(2)">Play song 2 </a>

和:

<a href="#2" onclick="changeSong(2)">Play song 2 </a>

那是错字吗?


实际上,它适用于console

document.getElementById("song");
<source id=​"song" src=​"horse.ogg" type=​"audio/​ogg">​
document.getElementById("song").src;
"chrome://newtab/horse.ogg"
document.getElementById("song").src = 'Hi.ogg';
"Hi.ogg"
document.getElementById("song").src;
"chrome://newtab/Hi.ogg"
于 2012-12-07T14:28:10.367 回答
0

You miss the name of the method in the call. This works:

<a href="#1" onclick="changeSong(1)">Play song 1 </a></br>
<a href="#2" onclick="changeSong(2)">Play song 2 </a>
于 2012-12-07T14:29:50.460 回答
0

what is your goal ? Do not tell me that you are trying to call the

changeSong(1)

on the "Play song 1" click.

just by reading your code I can say that change

<a href="#2" onclick="playSong(2)">Play song 2 </a>

by

<a href="#2" onclick="changeSong(2)">Play song 2 </a>

Or I'm missing something :/

Can you add some debug code into the

changeSong(n){
    document.getElementById("song").src="song"+n+".mp3";
}

an tell us if something is appening :)

于 2012-12-07T14:30:35.590 回答
0

我不熟悉 HTML5 ,但我可以找到两个错误。

  1. 你打电话给 playSong 而不是 changeSong

  2. 您应该声明 changeSong 是一个函数:

    function changeSong(n){
        document.getElementById("song").src = "song" + n + ".mp3";
    }
    
于 2012-12-07T14:31:32.207 回答