所以我是编码的新手,我只是想为自己做一个小项目。所有它都是一个按钮,当您按下它时,它将随机播放三种声音之一。但是出了点问题,我不知道是什么原因,有人知道吗?
欢呼声
<!DOCTYPE html>
<link rel="stylesheet" href="C:\Users\Owner\Google Drive\Code\Experiments\HTML-CSS-JAVASCRIPT\Random Sound\Code\Random Sounds.css">
<h1> Random Sounds </h1>
<button id="press"> Press me! </button>
<script>
var num = 0
function pressed() {
num = Math.floor(1 + 3 * Math.random())
if (num == 1) {
var audio1 = new Audio('C:\Users\Owner\Google Drive\Code\Experiments\HTML-CSS-JAVASCRIPT\Random Sound\Sounds\OFFICIAL Somewhere over the Rainbow
- Israel IZ Kamakawiwoʻole.mp3');
audio1.play()
} else if (num == 2) {
var audio2 = new Audio('C:\Users\Owner\Google Drive\Code\Experiments\HTML-CSS-JAVASCRIPT\Random Sound\Sounds\OFFICIAL Somewhere over the Rainbow
- Israel IZ Kamakawiwoʻole.mp3');
audio2.play()
} else {
var audio3 = new Audio('C:\Users\Owner\Google Drive\Code\Experiments\HTML-CSS-JAVASCRIPT\Random Sound\Sounds\OFFICIAL Somewhere over the Rainbow
- Israel IZ Kamakawiwoʻole.mp3');
audio3.play()
}
}
press.onclick = pressed
</script>
A
答案 0 :(得分:0)
解决方案在这里!
*实际上问题是您的音频源路径。
<!DOCTYPE html>
<link rel="stylesheet" href="C:/Users/Owner/Google Drive/Code/Experiments/HTML-CSS-JAVASCRIPT/Random Sound/Code/Random Sounds.css">
<h1> Random Sounds </h1>
<button id="press"> Press me! </button>
<script>
var num = 0
function pressed() {
num = Math.floor(1 + 3 * Math.random())
if (num == 1) {
var audio1 = new Audio('///C:/Users/Owner/Google Drive/Code/Experiments/HTML-CSS-JAVASCRIPT/Random Sound/Sounds/OFFICIAL Somewhere over the Rainbow
- Israel IZ Kamakawiwoʻole.mp3');
audio1.play()
} else if (num == 2) {
var audio2 = new Audio('///C:/Users/Owner/Google Drive/Code/Experiments/HTML-CSS-JAVASCRIPT/Random Sound/Sounds/OFFICIAL Somewhere over the Rainbow
- Israel IZ Kamakawiwoʻole.mp3');
audio2.play()
} else {
var audio3 = new Audio('///C:/Users/Owner/Google Drive/Code/Experiments/HTML-CSS-JAVASCRIPT/Random Sound/Sounds/OFFICIAL Somewhere over the Rainbow
- Israel IZ Kamakawiwoʻole.mp3');
audio3.play()
}
}
press.onclick = pressed
</script>