次のコードは、Safari を除くすべてのブラウザーで意図したとおりに機能します (Windows 7 ではバージョン 5.1.7 を使用)。
var texto01 = document.getElementById("texto01");
texto01.onkeyup = function(tecla){
if(tecla.keyCode == 13){ //If Enter is keyup
if((valorAnt[0] != texto01.value) && (texto01.value != "")){ //Check to see if answer has changed and field is not empty
valorAnt[0] = texto01.value; //Since value has change, set it to be new previous answer
if(texto01.value == "5747"){ //Correct answer user has to type
ctx.clearRect(739, 57, 35, 35);
ctx.drawImage(bienImg, 739, 57); //Tick image
var cBien = Math.ceil(Math.random()*2); //There are two audio files that can be played if answer is correct
var cSonidoB = "bien" + cBien;
document.getElementById(cSonidoB).play(); //play the "random" audio for right answer
}
else{ //Answer is wrong
ctx.clearRect(739, 57, 35, 35);
ctx.drawImage(malImg, 739, 57); //X mark image
var cMal = Math.ceil(Math.random()*3); //There are three audio files that can be played if answer is wrong
var cSonidoM = "mal" + cMal;
document.getElementById(cSonidoM).play(); //play the "random" audio for wrong answer
}
}
}
}//texto01
すべてのオーディオ タグは次のように宣言されます。
<audio id = "mal1" preload = "auto">
<source src="Sounds/mal01.ogg" type="audio/ogg"></source>
<source src="Sounds/mal01.mp3" type="audio/mpeg"></source>
</audio>
私は次のことを試みましたが成功しませんでした: 1) MP3 ソースを OGG の前に置きます。2) タイプを「audio/mp3」に変更 3) 再生するオーディオを選択する「ランダム」部分を削除し、再生するオーディオ ID を直接割り当てます。