简体   繁体   English

如何使用javascript打印在子手游戏中已经猜到的字母?

[英]How do I print the letters already guessed in the hangman game with javascript?

I am knew to programming as well as javascript and am working on the hangman game for my first js project. 我对编程以及JavaScript都很了解,并且正在为我的第一个js项目开发on子手游戏。 I can't seem to figure out how to show the user in the UI the letters they have already guessed. 我似乎无法弄清楚如何在用户界面中向用户显示他们已经猜到的字母。 I figured out the onkeyup function to show the most recent letter chosen but not all of them. 我想出了onkeyup函数来显示选择的最新字母,但不是全部。 Any suggestions? 有什么建议么?

 var alphabet = 
 ['a','b','c','d','e','f','g','h','g','h','i','j','k','l','m','n','o','p','q','r','s',
't','u','v','w','x','y','z'];
document.onkeyup = function(event) {
     var key_press = String.fromCharCode(event.keyCode);
     console.log(key_press);
     document.getElementById('guessed-letters').innerHTML = key_press;
};

Take what you have here 拿你在这里的东西

var alphabet = ['a','b','c','d','e','f','g','h','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];

document.onkeyup = function(event) {

  var key_press = String.fromCharCode(event.keyCode);
  console.log(key_press);
  document.getElementById('guessed-letters').innerHTML = key_press;
};

And just do this: 只需这样做:

var alphabet = ['a','b','c','d','e','f','g','h','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
var usedChars = [];

document.onkeyup = function(event) {

  var key_press = String.fromCharCode(event.keyCode);
  console.log(key_press);
  usedChars.push(key_press)
  document.getElementById('guessed-letters').innerHTML = usedChars.toString();
};

Changing this line: 更改此行:

document.getElementById('guessed-letters').innerHTML = key_press;

to this: 对此:

document.getElementById('guessed-letters').innerHTML += key_press;

should achieve what you want. 应该实现您想要的。 You'd be appending to the existing HTML instead of replacing it. 您将附加到现有HTML而不是替换它。

 var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split(''); var outputElement = document.getElementById('guessed-letters'); var guessedLetters = []; // stores the letters the user guessed document.addEventListener('keyup', function (event) { var key = event.key.toLowerCase(); if (alphabet.indexOf(key) !== -1) { // the key is a letter in the alphabet if (guessedLetters.indexOf(key) === -1) { // the key has not been guessed guessedLetters.push(key); var string = guessedLetters.join(''); // join the letters together to one single string outputElement.textContent = string; } } }); 
 <span id="guessed-letters"></span> 

Or using jQuery: 或使用jQuery:

 var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split(''); var outputElement = $('#guessed-letters'); var guessedLetters = []; // stores the letters the user guessed $(document).on('keyup', function (event) { var key = event.key.toLowerCase(); if (alphabet.indexOf(key) !== -1) { // the key is a letter in the alphabet if (guessedLetters.indexOf(key) === -1) { // the key has not been guessed guessedLetters.push(key); var string = guessedLetters.join(''); // join the letters together to one single string outputElement.text(string); } } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <span id="guessed-letters"></span> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM