当前位置: 首页 > news >正文

JS+H5打字练习器

实现功能

1.导入.TXT文件到打字练习内容(部分浏览器可能出于安全问题限制了这一步操作)
2.输入文本到打字练习内(弹出输入框,将要练习的内容输入至输入框)
3. 开始练习,并根据正误在打字练习内容文本上修改颜色(某个字正确显示绿色,某个字错误,显示红色,某个字没有打显示灰色)
4.开始练习、结束练习,全部输入完成后自动结束练习,也可提前手动结束练习,结束练习后弹窗用显示时和正确率。(区别在于一个全部输入完成且全部正确,一个提前结束无其他条件)

效果图

在这里插入图片描述在这里插入图片描述

分析

HTML 结构解析

  1. 根元素<!DOCTYPE html> 声明文档类型为HTML。
  2. html 元素:包含整个页面的内容,并设置语言为中文(lang="zh-CN")。
  3. head 元素:包含页面的元数据,如字符集(charset="UTF-8")、视口设置(viewport)和标题(<title>)。
  4. style 元素:包含页面的CSS样式。
    • 设置页面背景颜色、字体、布局样式等。
  5. body 元素:包含页面的可见内容。
    • 使用Flexbox布局使内容居中。
  6. .container:包含文本输入框和按钮,并设置样式。
  7. .buttons:包含一组按钮,并设置布局样式。
  8. .text-to-type:包含待输入的文本,并设置样式。
  9. input 元素:一个文本输入框,用于用户输入。

JavaScript 解析

  1. 变量声明

    • textToType:获取待输入文本的DOM元素。
    • inputText:获取用户输入文本的DOM元素。
    • importTextBtnaddTextBtnstartPracticeBtnendPracticeBtn:获取按钮的DOM元素。
    • originalText:存储原始待输入的文本。
    • startTime:存储开始练习的时间。
    • alertCount:存储弹窗次数。
    • correctCount:存储正确的字符数。
  2. inputText 输入事件监听器

    • 当用户在输入框中输入时,会触发此事件。
    • 获取用户当前输入的文本。
    • 初始化一个空字符串updatedText用于构建更新后的文本。
    • 如果startTime已设置,遍历原始文本和用户输入的文本:
      • 如果字符匹配,将字符标记为correct
      • 如果字符不匹配,将字符标记为wrong
    • 更新textToType的HTML内容。
    • 如果用户输入的文本与原始文本完全匹配,计算总用时和正确率,并通过弹窗显示结果。
    • 重置输入框和startTime
  3. importTextBtn 点击事件监听器

    • 创建一个文件输入元素,允许用户选择.txt文件。
    • 当文件被选中后,使用FileReader读取文件内容。
    • 读取完成后,将文本内容赋值给originalText并更新textToType
  4. addTextBtn 点击事件监听器

    • 弹出一个输入框让用户输入文本。
    • 如果用户输入了文本,将其赋值给originalText并更新textToType
  5. startPracticeBtn 点击事件监听器

    • 设置startTime为当前时间。
    • 清空输入框。
    • 更新textToType的HTML内容为原始文本。
  6. endPracticeBtn 点击事件监听器

    • 如果startTime已设置,计算总用时和正确率,并通过弹窗显示结果。
    • 重置startTime和输入框。

完成源代码

<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>打字练习</title><style>body {font-family: Arial, sans-serif;background-color: #f0f0f0;display: flex;justify-content: center;align-items: center;height: 100vh;margin: 0;}.container {background-color: white;padding: 20px;border-radius: 15px;box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);max-width: 600px;width: 100%;text-align: center;}.buttons {display: flex;justify-content: space-around;margin-bottom: 20px;}.buttons button {background-color: black;color: white;border: none;border-radius: 5px;box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);padding: 10px 20px;cursor: pointer;font-size: 14px;}.text-to-type {color: gray;margin-bottom: 20px;font-size: 20px;}.text-to-type span.correct {color: green;}.text-to-type span.wrong {color: red;}input[type="text"] {width: 100%;padding: 10px;border-radius: 5px;border: 1px solid #ccc;font-size: 18px;}</style>
</head>
<body><div class="container"><div class="buttons"><button id="importText">导入文本</button><button id="addText">添加文本</button><button id="startPractice">开始练习</button><button id="endPractice">结束练习</button></div><div class="text-to-type" id="textToType">这是一个打字练习的例子</div><input type="text" id="inputText" placeholder="开始打字..." /></div><script>const textToType = document.getElementById('textToType');const inputText = document.getElementById('inputText');const importTextBtn = document.getElementById('importText');const addTextBtn = document.getElementById('addText');const startPracticeBtn = document.getElementById('startPractice');const endPracticeBtn = document.getElementById('endPractice');let originalText = textToType.innerText;let startTime = null;let alertCount = 0;let correctCount = 0;inputText.addEventListener('input', () => {const typedText = inputText.value;let updatedText = '';correctCount = 0;if (startTime) {for (let i = 0; i < originalText.length; i++) {if (i < typedText.length) {if (typedText[i] === originalText[i]) {updatedText += `<span class="correct">${originalText[i]}</span>`;correctCount++;} else {updatedText += `<span class="wrong">${originalText[i]}</span>`;}} else {updatedText += originalText[i];}}textToType.innerHTML = updatedText;}if (typedText === originalText && startTime) {const endTime = new Date();const timeTaken = ((endTime - startTime) / 1000).toFixed(2);const accuracy = ((correctCount / originalText.length) * 100).toFixed(2);alert(`总用时间: ${timeTaken} 秒\n正确率: ${accuracy}%`);alertCount++;if (alertCount < 3) {alert(`这是第 ${alertCount} 次弹窗`);}inputText.value = '';startTime = null;}});importTextBtn.addEventListener('click', () => {const fileInput = document.createElement('input');fileInput.type = 'file';fileInput.accept = '.txt';fileInput.onchange = e => {const file = e.target.files[0];const reader = new FileReader();reader.onload = event => {originalText = event.target.result.trim();textToType.innerText = originalText;};reader.readAsText(file);};fileInput.click();});addTextBtn.addEventListener('click', () => {const userInput = prompt('请输入要练习的文本:');if (userInput) {originalText = userInput.trim();textToType.innerText = originalText;}});startPracticeBtn.addEventListener('click', () => {startTime = new Date();inputText.value = '';textToType.innerHTML = originalText;});endPracticeBtn.addEventListener('click', () => {if (startTime) {const endTime = new Date();const timeTaken = ((endTime - startTime) / 1000).toFixed(2);const accuracy = ((correctCount / originalText.length) * 100).toFixed(2);alert(`练习结束\n总用时间: ${timeTaken} 秒\n正确率: ${accuracy}%`);startTime = null;inputText.value = '';} else {alert('请先开始练习!');}});</script>
</body>
</html>

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • JAVA-案列练习-ATM项目
  • DDOS攻击有哪些攻击手段?
  • linux内核,中断上下文,中断下半部的处理2种方法: tasklet_schedule,工作队列workqueue
  • docker安装与container基本使用
  • android 音频播放器,(一)SoundPool音频播放实例
  • linux 原子操作,信号量,互斥锁mutex,自旋锁splock
  • 嵌入式数据库 sqlite3
  • 中央处理器CPU
  • 力扣第五十题——Pow(x,n)
  • Linux中yum、rpm、apt-get、wget的区别,yum、rpm、apt-get常用命令,CentOS、Ubuntu中安装wget
  • 【Linux】-----进度条小程序
  • 计算机毕业设计选题推荐-课程教学辅助系统-Java/Python项目实战
  • 【教程】Python语言的地球科学常见数据——植被指数数据-GIMMS NDVI的处理
  • Python基础教程(二)字符串和函数
  • CSS学习 01 利用鼠标悬停制造文本隐藏效果
  • 2018以太坊智能合约编程语言solidity的最佳IDEs
  • Angular 响应式表单之下拉框
  • - C#编程大幅提高OUTLOOK的邮件搜索能力!
  • CSS盒模型深入
  • CSS居中完全指南——构建CSS居中决策树
  • CSS魔法堂:Absolute Positioning就这个样
  • js操作时间(持续更新)
  • nginx(二):进阶配置介绍--rewrite用法,压缩,https虚拟主机等
  • orm2 中文文档 3.1 模型属性
  • scala基础语法(二)
  • SpringCloud集成分布式事务LCN (一)
  • springMvc学习笔记(2)
  • Traffic-Sign Detection and Classification in the Wild 论文笔记
  • Vue实战(四)登录/注册页的实现
  • 来,膜拜下android roadmap,强大的执行力
  • 深入浏览器事件循环的本质
  • 数据仓库的几种建模方法
  • 学习Vue.js的五个小例子
  • 【干货分享】dos命令大全
  • 翻译 | The Principles of OOD 面向对象设计原则
  • ​Spring Boot 分片上传文件
  • ​猴子吃桃问题:每天都吃了前一天剩下的一半多一个。
  • #预处理和函数的对比以及条件编译
  • (11)MSP430F5529 定时器B
  • (2024,Vision-LSTM,ViL,xLSTM,ViT,ViM,双向扫描)xLSTM 作为通用视觉骨干
  • (4.10~4.16)
  • (DenseNet)Densely Connected Convolutional Networks--Gao Huang
  • (Repost) Getting Genode with TrustZone on the i.MX
  • (附源码)计算机毕业设计SSM疫情社区管理系统
  • (个人笔记质量不佳)SQL 左连接、右连接、内连接的区别
  • (简单有案例)前端实现主题切换、动态换肤的两种简单方式
  • (六)库存超卖案例实战——使用mysql分布式锁解决“超卖”问题
  • (十八)Flink CEP 详解
  • (使用vite搭建vue3项目(vite + vue3 + vue router + pinia + element plus))
  • (文章复现)基于主从博弈的售电商多元零售套餐设计与多级市场购电策略
  • (续)使用Django搭建一个完整的项目(Centos7+Nginx)
  • (一)搭建springboot+vue前后端分离项目--前端vue搭建
  • (转)微软牛津计划介绍——屌爆了的自然数据处理解决方案(人脸/语音识别,计算机视觉与语言理解)...
  • * CIL library *(* CIL module *) : error LNK2005: _DllMain@12 already defined in mfcs120u.lib(dllmodu
  • **CI中自动类加载的用法总结