我正在尝试使用 JQuery 中的 on("keydown") 函数执行 if else 语句。
本质上是试图根据是否按下一组组中的特定键来更改 HTML。
这是代码:
keyCodeToChar = {8:"Backspace",9:"Tab",13:"Enter",16:"Shift",17:"Ctrl",18:"Alt",19:"Pause/Break",20:"Caps Lock",27:"Esc",32:"Space",33:"Page Up",34:"Page Down",35:"End",36:"Home",37:"Left",38:"Up",39:"Right",40:"Down",45:"Insert",46:"Delete",48:"0",49:"1",50:"2",51:"3",52:"4",53:"5",54:"6",55:"7",56:"8",57:"9",65:"A",66:"B",67:"C",68:"D",69:"E",70:"F",71:"G",72:"H",73:"I",74:"J",75:"K",76:"L",77:"M",78:"N",79:"O",80:"P",81:"Q",82:"R",83:"S",84:"T",85:"U",86:"V",87:"W",88:"X",89:"Y",90:"Z"};
var letter = keyCodeToChar[e.which || e.keyCode];
document.getElementById(letter).play();
var cool_letters = ["L","E","M","Z"];
if ($.inArray(letter, cool_letters) == true) {
var myHtml = "<p class='" + letter + "''>" + letter + "</p>";
$('.key').append(myHtml)
.css("display", "block")
.css("color", random_colour);
} else {
var myHtml = letter;
$('.key').append(myHtml)
.css("display", "block")
.css("color", random_colour);
};
出于某种原因,无论我尝试什么,我都无法让 JS 识别 var cool_letters 中的各个字母,并正确执行 if...else 块。感激地收到任何帮助。