所以我是新來的,需要一些(也許很多)的幫助。我希望能夠按下一個按鈕並從中隨機播放聲音。我認爲我到目前爲止所做的是正確的,但我真的不知道如何使它工作。Javascript按鈕播放隨機聲音
這裏是我的JavaScript
function mySounds()
{
var x = Math.floor((Math.random() * 10) + 1);
var sound = ["sound1", "sound2", "sound3", "sound4", "sound5", "sound6", "sound7", "sound8", "sound9", "sound10"];
}
sound1 = 1;
sound2 = 2;
sound3 = 3;
sound4 = 4;
sound5 = 5;
sound6 = 6;
sound7 = 7;
sound8 = 8;
sound9 = 9;
sound10 = 10;
if (x === sound1){
function sound1play(){
var sound1 = new Audio();
sound1.src = "Pokemon.mp3";
sound1.play();
}
}
if (x === sound2){
function sound2play() {
var sound2 = new Audio();
sound2.src = "Wonderwall.mp3";
sound2.play();
}
}
if (x === sound3){
function sound3play() {
var sound3 = new Audio();
sound3.src = "Tacobell.mp3";
sound3.play();
}
}
if (x === sound4){
function sound4play() {
var sound4 = new Audio();
sound4.src = "Starwars.mp3";
sound4.play();
}
}
if (x === sound5){
function sound5play() {
var sound5 = new Audio();
sound5.src = "Mcdonalds.mp3";
sound5.play();
}
}
if (x === sound6){
function sound6play() {
var sound6 = new Audio();
sound6.src = "KFC.mp3";
sound6.play();
}
}
if (x === sound7){
function sound7play() {
var sound7 = new Audio();
sound7.src = "Salvia.mp3";
sound7.play();
}
}
if (x === sound8){
function sound8play() {
var sound8 = new Audio();
sound8.src = "Allstar.mp3";
sound8.play();
}
}
if (x === sound9){
function sound9play() {
var sound9 = new Audio();
sound9.src = "Funtime.mp3";
sound9.play();
}
}
if (x === sound10){
function sound10play() {
var sound10 = new Audio();
sound10.src = "Clothes.mp3";
sound10.play();
}
}
document.getElementById('soundbutton').addEventListener('click', soundplay);
這裏是我的HTML
<a href="#" id="soundbutton">Press</a>
<script type="text/javascript" src="popup.js"></script>
不工作的東西嗎?你只描述了目標,而不是你面對的具體問題。 –