Javascript更改音乐来源

时间:2017-01-13 17:19:56

标签: javascript html audio

我想让我的javascript改变我的html音频的来源。 我在stackoverflow上搜索但是找不到我想要的东西。

这是我的HTML代码。我现在不用css。

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>HigherLower</title>
</head>

<body>

  <h1> welcome to my game. This game is about selecting if a number is getting higher or lower than the previous number.
    Click this button to get started.</h1>

  <audio id="currentsong" loop autoplay controls src="trol.mp3"></audio>

  <button id="songbutton" onclick="nextsong()">next song</button>


  <script src="javascript.js"></script>
</body>

</html>

这是我的javascript代码:

    /**
  * Created by Jasper on 12-1-2017.
  */
 var currentsong = document.getElementById('currentsong');
 var nextbutton = document.getElementById('songbutton');

 currentsong = 1;
 currentsong = 2;
 currentsong = 3;

 function nextsong() {
   if (currentsong = 1) {

     innerHTML = currentsong.src = 'trol.mp3';
   } else {
     currentsong.innerHTML = currentsong.src = 'paradise.mp3';
   }
 }

我想在按钮上点击音乐。提前致谢

2 个答案:

答案 0 :(得分:0)

更改

currentsong.innerHTML=currentsong.src='paradise.mp3';

currentsong.src = 'paradise.mp3'

答案 1 :(得分:0)

以下是我可以尝试的内容..无论您需要修复代码,因为您使用的是与您检索引用的计数器var相同的变量 你的元素'currentsong'

curl -X POST -i --header 'Content-Type: application/x-www-form-urlencoded' -d 'message=frickin' 'http://localhost:4042/echo'
HTTP/1.1 200 OK
Date: Fri, 13 Jan 2017 17:32:03 GMT
Content-Type: text/plain;charset=ISO-8859-1
Content-Length: 14
Server: Jetty(7.6.13.v20130916)

message: frickin