2
目前我有:使用按鍵播放聲音的js
is1Pressed = false,
is2Pressed = false,
is3Pressed = false,
is4Pressed = false,
is5Pressed = false,
is6Pressed = false,
p = document.getElementById('p');
function loop() {
if(is1Pressed == true){
console.log("1");
p.innerHTML = "<audio autoplay='autoplay' src='button1.wav'/>";
}
else if(is2Pressed == true) {
console.log=("2");
p.innerHTML = "<audio autoplay='autoplay' src='button1.wav'/>";
}
else if(is3Pressed == true) {
p.innerHTML = "<audio autoplay='autoplay' src='button1.wav'/>";
}
else if(is4Pressed == true) {
p.innerHTML = "<audio autoplay='autoplay' src='button1.wav'/>";
}
else if(is5Pressed == true) {
p.innerHTML = "<audio autoplay='autoplay' src='button1.wav'/>";
}
else
{
p.innerHTML = "<audio autoplay='autoplay' src='button1.wav'/>";
}
}
document.addEventListener('keydown', function (e) {
switch (e.keyCode) {
// 1
case 49:
is1Pressed = true;
break;
// 2
case 50:
is2Pressed = true;
break;
// 3
case 51:
is3Pressed = true;
break;
// 4
case 52:
is4Pressed = true;
break;
// 5
case 53:
is5Pressed = true;
break;
//6
case 54:
is6Pressed = true;
break;
}
}, false);
document.addEventListener('keyup', function (e) {
switch (e.keyCode) {
// 1
case 49:
is1Pressed = false;
break;
// 2
case 50:
is2Pressed = false;
break;
// 3
case 51:
is3Pressed = false;
break;
// 4
case 52:
is4Pressed = false;
break;
// 5
case 53:
is5Pressed = false;
break;
//6
case 54:
is6Pressed = false;
break;
}
}, false);
var framesPerSecond = 60,
intervalId = setInterval(loop, framesPerSecond/1000);
但是,當我按下1個鍵播放樣本聲音的的console.log「1」上運行的聲音從來沒有初始化。我究竟做錯了什麼?此外,當頁面處於非活動狀態時,聲音循環。
是否在瀏覽器控制檯中顯示任何錯誤?實際上是否有ID爲「p」的元素?此外 - 記住[我說過明確的'if'聲明反對TRUE;檢查(http://stackoverflow.com/a/8622826/139010)...... – 2011-12-24 05:43:37
沒有錯誤顯示的,是有一個段落「p」的編號 – JoshMWilliams 2011-12-24 05:46:16
您是否使用HTML5文檔類型?支持「