JavaScript看起来像这样:
function playsound(button) {
switch(button){
case "button1":
switch(soundbank){
case 0:
document.getElementById("audio1").load();
document.getElementById("audio1").play();
break;
case 1:
...
...
case "button2":
switch(soundbank){
case 0:
document.getElementById("audio2").load();
document.getElementById("audio2").play();
break;
case "button3":
switch(soundbank){
case 0:
document.getElementById("audio3").load();
document.getElementById("audio3").play();
break;
case "button4":
switch(soundbank){
case 0:
document.getElementById("audio4").load();
document.getElementById("audio4").play();
break;
case "..."
...
...
}}
像这样的HTML:
<button onclick="playsound(this.id)" id="button1" </button>
<button onclick="playsound(this.id)" id="button2" </button>
<button onclick="playsound(this.id)" id="button3" </button>
<button onclick="playsound(this.id)" id="button4" </button>
<audio id="audio1">
<source src="audio1.wav">
</audio>
<audio id="audio2">
<source src="audio2.wav">
</audio>
<audio id="audio3">
<source src="audio3.wav">
</audio>
<audio id="audio4">
<source src="audio4.wav">
</audio>
我想把它的大部分放在循环中,而不是有1000行的开关盒和If Else但是我无法解决它。有什么建议?非常感谢。
答案 0 :(得分:0)
使用字符串操作可以做些什么。
document.getElementById("audio"+button.substring(6)).load();