javascript - eventlistener 不更新全局变量
问题描述
我正在尝试将 ascii 转换为 char 并将 char 转换为 ascii 翻译器
const outerWheel = document.querySelector('.outer-wheel');
const innerWheel = document.querySelector('.inner-wheel');
const centerWheel = document.querySelector('.wheel-center');
let key = document.querySelector('.key');
key.addEventListener('input', function(e){
let key = e.target.value
outerWheel.style.transform = `translate(-50%, -50%) rotate(0deg)`
innerWheel.style.transform = `translate(-50%, -50%) rotate(${key * 14.4}deg)`
centerWheel.style.transform = `translate(-50%, -50%) rotate(0deg) rotate(${key * -14.4}deg)`
})
let shift = 0
let str = ''
let strBox = document.querySelector('.encrypt-text')
let shiftBox = document.querySelector('.encrypt-key')
shiftBox.addEventListener('input', function (e) {
shift = e.target.value // this is supposed for shifting the ascii codes
})
console.log(shift)
strBox.addEventListener('input', function(e){
let str = e.target.value // i want this value to calculate the end of the for loop
console.log(str)
let codedStr = '';
for (let i = 0; i < str.length; i++) {
let char = str.charAt(i)
let ascii = char.charCodeAt(0)
let shiftedAscii = ascii + shift
switch(true) {
case (ascii >= 65 && ascii <= 90):
if (shiftedAscii < 65) {shiftedAscii += 26}
if (shiftedAscii > 90) {shiftedAscii -= 26}
break;
case (ascii >= 97 && ascii <= 122):
if (shiftedAscii < 97) {shiftedAscii += 26}
if (shiftedAscii > 122) {shiftedAscii -= 26}
break;
default:
console.log('nem betu')
break;
}
let codedChar = String.fromCharCode(shiftedAscii)
codedStr += codedChar;
}
codedStr= codedStr.substr(codedStr.length - str.length)
const encryptedText = document.querySelector('.encrypted-text')
encryptedText.innerHTML = codedStr
解决方案
推荐阅读
- function - 相当于 Go 中 Python 的“named_function.__name__”
- python - 出现新的csv文件时如何自动运行python脚本
- vue.js - 无法从 Vue Computed 属性访问 element.scrollLeft 位置?如何?
- sharepoint - 如何通过图形 API 检索 SharePoint 现代网站(团队网站和通信网站)的分类属性?
- java - 如何从微调器的下拉列表中隐藏索引为 0 的元素
- javascript - 如何在javascript中的函数名称中包含变量(字符串)?
- r - R-我如何使用地图函数来计算小标题中许多分组列的平均值
- python - 对给定 id 的 pandas 列中的最新值进行分组
- optimization - 如何不通过 GraphQL 暴露重复的(规范化?)节点?
- flask - Flask SQLAlchemy对具有给定值的多个列的唯一约束