javascript - 使用 jQuery 要求用户按下键盘上的每个字母和数字

标签 javascript jquery dom

我需要使用 jQuery 创建一个游戏,要求用户按下键盘上的每个字母和数字。

我首先创建一个包含 26 个字母和 10 个数字的数组,并将它们显示在屏幕上。这应该使用 jQuery 追加到 DOM 一次来完成,而不是为每个字母单独追加。当用户按下某个键时,我需要检查它是否被按下。如果已按下,则使字母或数字变灰。如果之前按下过该键,请告知用户。按下所有字符后,我需要让用户知道他们赢得了游戏,该消息应使用 jQuery 显示。

这是迄今为止我在 DOM 上显示字母的所有代码,我真的不知道从这里开始:

var array = [];
var func = function(num1,num2){

    for(var i = num1; i <= num2; i++){
        array.push(String.fromCharCode(i));
    }

  for(var i = 0; i < array.length; i++){
  array[i].className= array[i];
  }
    return(array);
}

func(65,90)
func(48,57)
String.fromCharCode(65,66,67,68)

var para = document.createElement("p");
para.innerHTML=array
document.body.appendChild(para);

最佳答案

这是实现您想要的目标的一种方法:

假设 html 文件包含:

<div id="board"></div>
<div id="log"></div>

对应的javascript代码:

var array = [];
var func = function(num1,num2){
    for(var i = num1; i <= num2; i++){
        array.push(i);
    }
}

func(65,90)
func(48,57)
array = [].concat(array, [65,66,67,68])
console.clear()
console.log(array)

var buttons = []
array.forEach(function(item) {
  // create a button with the charcode, and an id
  var btn = $('<button>').html(String.fromCharCode(item)).prop('id', item)
  buttons.push(btn)
})
// appending all elements at once to the board as per your specification
$('#board').append(buttons)

var pressed = []
// listenning on window 'keypress' event
$(document).on('keypress', function(ev) {
  console.log(ev)
  if(ev.hasOwnProperty('charCode') || ev.hasOwnProperty('keyCode')) {
    // get the charcode pressed
    var code = ev.charCode || ev.keyCode;
    // quit  if code has already been pressed
    if(pressed.indexOf(code) > -1) return;
    pressed.push(code); // adding the code to the pressed array
    // changing button color
    $('#' + code).css({'color': 'red', 'background-color': 'grey'});
    $('#' + code).prop('disabled', 'disabled');
    $("#log").html(array.length - pressed.length + ' remaining')
  }
})

带有nice fiddle !

编辑:较短版本

参见Updated fiddle

CSS 文件:

.disabled {
  color: red;
  background-color: gray
}

HTML 文件:

<div id="board"></div>

JS文件:

var array = []
var func = function(num1,num2){
    for(var i = num1; i <= num2; i++){
        // create a button with the charcode, and an id
        var btn = $('<button>').html(String.fromCharCode(i)).prop('id', i)
  array.push(btn)
    }
}
func(65,90)
func(48,57)

// appending button to the board
$('#board').append(array)

var pressed = []
// listenning on window 'keypress' event
$(document).on('keypress', function(ev) {
    // get the charcode pressed
    var code = ev.charCode || ev.keyCode || 0;
    if(pressed.indexOf(code) > -1) return
    // quit  if code has already been pressed
    pressed.push(code); // adding the code to the pressed array
    // changing button color via css class
    $('#' + code).addClass('disabled')
})

关于javascript - 使用 jQuery 要求用户按下键盘上的每个字母和数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36377526/

相关文章:

javascript - 使用按钮从数组打印

javascript - Angular JS。如何公开查看应用程序级常量?

javascript - 为什么按价格排序不起作用?

java - 如何解析 HTML 中存在的以下字符串并在 Java 中构建 DOM 树?

javascript - jQuery:DIV 上的幻灯片动画改变其大小

jQuery 父选择器

javascript - spotfire 网络播放器中的 Iron Python 脚本

javascript - 使用 jQuery 粗体链接到父 URL(在 WordPress 上)

javascript - 使用jquery数据表我无法在不破坏FixedHead的情况下使单元格colspan = 3

javascript - jQuery 问题无法检索或设置属性, 'undefined'