Javascript 键入游戏--希望键入的所有字母都是小写
我正在尝试将字段中键入的所有字母转换为小写。我知道这很简单,但就我个人而言,我无法让它发挥作用。我想如果我把“word input.toLowerCase()”放在match words函数下,它应该使字母小写,但它不起作用。以下是我所拥有的:Javascript 键入游戏--希望键入的所有字母都是小写,javascript,javascript-objects,Javascript,Javascript Objects,我正在尝试将字段中键入的所有字母转换为小写。我知道这很简单,但就我个人而言,我无法让它发挥作用。我想如果我把“word input.toLowerCase()”放在match words函数下,它应该使字母小写,但它不起作用。以下是我所拥有的: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<!-- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.2.1/css/bootstrap.min.css"> -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css">
<title>One Piece WordBeater</title>
</head>
<body>
<body class="bg-dark text-white">
<header class="bg-secondary text-center p-3 mb-5">
<h1>One Piece Word Beater</h1>
</header>
<div class="container text-center">
<!--Word & Input-->
<div class="row">
<div class="col-md-3 mx-auto">
<p class="lead">Current Level: <span class="text-info" id="levels"></span></p>
</div>
<div class="col-md-6 mx-auto">
<p class="lead">Type the Given Word Within <span class="text-success" id="seconds">5</span> Seconds. </p>
<h2 class="display-2 mb-5" id="current-word">luffy</h2>
<input type="text" class="form-control form-control-lg" placeholder="Start Typing..." id="word-input" autofocus>
<h4 class="mt-3" id="message"></h4>
<br>
<div class="btn-group">
<button type="button" class="btn btn-warning mr-5" id="start"><b>Let's Play!</b></button>
<button type="button" class="btn btn-danger" id="reset"><b>Reset Game</b></button>
</div>
</div>
<div class="col-md-3 mx-auto">
<p class="lead">High Score: <span class="text-info" id="highsocre"></span></p>
</div>
</div>
<!--Time and Columns-->
<div class="row mt-5">
<div class="col md-6">
<h3>Time left: <span id="time">0</span></h3>
</div>
<div class="col md-6">
<h3>Score: <span id="score"> 0 </span></h3>
</div>
</div>
<!--Instructions-->
<div class="row mt-5">
<div class="col md-12">
<div class="card card-body bg-secondary text-white">
<h5>Instructions</h5>
<p>Type each word in the given amount of seconds to score. To play again, just type the current word. Your score
will reset</p>
</div>
</div>
</div>
<!--Level Selector-->
<div class = "row mt-5">
<div class="col md-12">
<div class="card card-body bg-secondary text-white">
<h5>Select the Difficulty</h5>
<div class="row mt-5">
<div class="col md-12">
<div class="btn-group">
<button type="button" class="btn btn-success mr-2" id="easy">Easy</button>
<button type="button" class="btn btn-primary mr-2" id="medium">Medium</button>
<button type="button" class="btn btn-danger" id="hard">Hard</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.2.1/js/bootstrap.min.js"></script>
<script src="index.js"></script>
</body>
</html>
// window.addEventListener('load', init);
//Start Game
// const startbtn = document.getElementById('start');
// global Varibales
//Avaible levels
const levels = {
easy: 5,
medium: 3,
hard: 2,
}
//to cchange level
const currentLevel = levels.easy;
let time = currentLevel;
let score = 0;
let isPLaying;//initalzies game...true if game is on false if game is off
//DOM Elements
const wordInput = document.querySelector('#word-input');
const currentWord = document.querySelector('#current-word');
const scoreDisplay = document.querySelector('#score');
const timeDisplay = document.querySelector('#time');
const message = document.querySelector('#message');
const seconds = document.querySelector('#seconds');
const levelDisplay = document.querySelector('#levels');
const words = [
'luffy',
'zoro',
'shanks',
'nami',
'brook',
'chooper',
'sanji',
'franky',
'jinbe',
'carrot',
'pekoms',
'ace',
'sabo',
'robin',
'bellamy',
'crocodile',
'merry',
'yonko',
'camie',
'nefertari',
'raizo',
'momo',
'law',
'dracule',
'boa',
'buggy',
'golroger',
'bigmom',
'smoker',
'kaido'
];
//initialize Game
// function init() {
// //Show number of seconds
// seconds.innerHTML = currentLevel;
// //load a word from array
// showWord(words);
// //Start Matching on word input
// wordInput.addEventListener('input', startMatch);
// //Call countdown every second
// setInterval(countdown, 1000);
// //Check status
// setInterval(checkStatus, 50)
// }
window.onload = function init() {
//start button
const startBtn = document.getElementById('start')
startBtn.onclick = function() {
//Show number of seconds
seconds.innerHTML = currentLevel;
//load a word from array
showWord(words);
//Start Matching on word input
wordInput.addEventListener('input', startMatch);
//Call countdown every second
setInterval(countdown, 1000);
//Check status
setInterval(checkStatus, 50)
}
}
//level Buttons
//Easy Mode
// document.getElementById('easy').addEventListener('click', easyMode);
// function easyMode(levels) {
// }
//Start Match
function startMatch() {
if(matchWords()){
isPLaying = true;
time = currentLevel + 1;
showWord(words);
wordInput.value='';
score++;
}
//if score negative -1 display 0
if(score === -1){
scoreDisplay.innerHTML = 0;
}else{
scoreDisplay.innerHTML = score;
}
}
//Match Current Word to word imput
function matchWords(){
if(wordInput.value === currentWord.innerHTML) {
message.innerHTML = 'Correct!!!';
return true;
} else {
message.innerHTML = '';
return false;
}
}
//Pick & Show random word
function showWord(words) {
//Generate random array index
const randIndex = Math.floor(Math.random() * words.length);
//Output random word
currentWord.innerHTML = words[randIndex];
}
//Countdown Timer
function countdown() {
//Make sure time is not run out
if(time > 0) {
//Decrease time
time--;
}else if(time === 0) {
//Game Over
isPLaying = false;
}
//Show time
timeDisplay.innerHTML = time;
}
//Check game Status
function checkStatus() {
if(!isPLaying && time === 0){
message.innerHTML = 'Game Over!!';
score = -1;
}
}
//reset after game over
一体式打字器
一体式打字器
当前水平:
在5秒内键入给定的单词
卢菲
我们来玩吧!
重置游戏
高分:
剩余时间:0
分数:0
说明书
在给定的秒数内键入每个单词进行评分。要再次播放,只需键入当前单词。你的分数
将重置
选择难度
容易的
中等
硬的
//addEventListener('load',init);
//开始比赛
//const startbtn=document.getElementById('start');
//全球Varibales
//可用级别
常量级别={
简单:5,
中位数:3,
硬:2,,
}
//更改级别
const currentLevel=levels.easy;
让时间=电流水平;
分数=0;
让我来播放//initalzies游戏…如果游戏打开,则为true如果游戏关闭,则为false
//DOM元素
const wordInput=document.querySelector(“#word input”);
const currentWord=document.querySelector(“#当前单词”);
const scoresplay=document.querySelector(“#score”);
const timeDisplay=document.querySelector(“#time”);
const message=document.querySelector(“#message”);
const seconds=document.querySelector('#seconds');
const levelDisplay=document.querySelector(“#levels”);
常量字=[
“路飞”,
“佐罗”,
"香克利",,
"纳米",,
“布鲁克”,
“chooper”,
"三记",,
“弗兰基”,
“jinbe”,
“胡萝卜”,
“pekoms”,
“A”,
"沙波",,
“罗宾”,
“贝拉米”,
“鳄鱼”,
“快乐”,
“yonko”,
“卡米”,
“内弗塔里”,
“raizo”,
“莫莫”,
"法律",,
“德拉库勒”,
"boa",,
“马车”,
“golroger”,
“大妈”,
“吸烟者”,
“凯多”
];
//初始化游戏
//函数init(){
////显示秒数
//seconds.innerHTML=当前级别;
////从数组中加载一个字
//showWord(单词);
////在输入单词时开始匹配
//addEventListener('input',startMatch);
////每秒呼叫倒计时
//设置间隔(倒计时,1000);
////检查状态
//设置间隔(检查状态,50)
// }
window.onload=函数init(){
//开始按钮
const startBtn=document.getElementById('start')
startBtn.onclick=函数(){
//显示秒数
seconds.innerHTML=当前级别;
//从数组中加载一个字
showWord(单词);
//开始匹配单词输入
addEventListener('input',startMatch);
//每秒钟打一次倒计时
设置间隔(倒计时,1000);
//检查状态
设置间隔(检查状态,50)
}
}
//水平按钮
//简易模式
//document.getElementById('easy')。addEventListener('click',easyMode);
//功能easyMode(级别){
// }
//开始比赛
函数startMatch(){
if(匹配词()){
isplay=true;
时间=电流电平+1;
showWord(单词);
wordInput.value='';
分数++;
}
//如果得分为负-1,则显示0
如果(分数==-1){
scoreDisplay.innerHTML=0;
}否则{
scoreDisplay.innerHTML=分数;
}
}
//将当前单词与输入单词匹配
函数匹配词(){
if(wordInput.value==currentWord.innerHTML){
message.innerHTML='正确!!!';
返回true;
}否则{
message.innerHTML='';
返回false;
}
}
//选择并显示随机单词
函数showWord(words){
//生成随机数组索引
const randIndex=Math.floor(Math.random()*words.length);
//输出随机字
currentWord.innerHTML=单词[randIndex];
}
//倒数计时器
函数倒计时(){
//确保时间没有用完
如果(时间>0){
//缩短时间
时间--;
}否则如果(时间===0){
//游戏结束
isplay=false;
}
//表演时间
timeDisplay.innerHTML=时间;
}
//检查游戏状态
函数checkStatus(){
如果(!isplay&&time==0){
message.innerHTML='游戏结束!!';
得分=-1;
}
}
//游戏结束后重置
您只需使用CSS即可完成此操作
而在css文件中呢
.lowercase{text transform:lowercase}
然而,这也有不利的一面。发送到后端的值仍将是输入文本的实际值
如果希望确保以小写形式将输入发送到后端,我建议使用javascript将字符串转换为小写形式
因此,在您的例子中,变量'wordInput'具有对输入元素的引用。为了得到它的价值,你需要
if(wordInput.value === currentWord.innerHTML) {
if(wordInput.value.toLowerCase() === currentWord.innerHTML) {