javascript - 如何使输入再次可编辑?
问题描述
我正在尝试制作一个无需按下按钮即可立即转换的数字转换器。这是迄今为止在codepen上的代码,请测试它(首先使用十进制输入而不是使用二进制输入,忽略它们还没有功能的八进制和十六进制):
https://codepen.io/ahmadbenos/full/eYprJyW
const binary = document.getElementById("binary");
const octal = document.getElementById("octal");
const decimal = document.getElementById("decimal");
const hex = document.getElementById("hex");
var binaryIsClicked = false;
var octalIsClicked = false;
var hexIsClicked = false;
var decimalIsClicked = false;
var decimalConvert = setInterval(convertDecimal, 50);
function convertDecimal() {
binary.value = Number(decimal.value).toString(2);
octal.value = Number(decimal.value).toString(8);
hex.value = Number(decimal.value).toString(16);
if (decimal.value === "") {
binary.value = "";
octal.value = "";
hex.value = "";
}
}
binary.addEventListener('click', function() {
clearInterval(decimalConvert);
binaryIsClicked = true;
var binaryConvert = setInterval(convertBinary, 50);
function convertBinary() {
decimal.value = parseInt(binary.value, 2);
octal.value = parseInt(binary.value, 2).toString(8);
hex.value = parseInt(binary.value, 2).toString(16);
if (binary.value === "") {
decimal.value = "";
octal.value = "";
hex.value = "";
}
}
});
所以问题来了,如果你先在十进制字段中输入一个数字 = 正常工作
之后您转到二进制输入字段并编辑/输入一个新数字=正常工作
之后,您再次进入十进制输入字段,但如果您尝试编辑/键入您不能,它会停留在前一个间隔(二进制间隔)。
有什么问题 ?我再次单击十进制字段后尝试清除二进制间隔,但它不起作用。
解决方案
我在那里修复了它:
//elements
const binary = document.getElementById("binary");
const octal = document.getElementById("octal");
const decimal = document.getElementById("decimal");
const hex = document.getElementById("hex");
//have no idea what these are for
var binaryIsClicked = false;
var octalIsClicked = false;
var hexIsClicked = false;
var decimalIsClicked = false;
//intervals not set yet, will be in event handlers functions
var decimalConvert;
var binaryConvert;
//event handlers
decimal.addEventListener('click', () =>{
//sets decimalConvert interval and clearing binaryConvert interval
decimalConvert = setInterval(convertDecimal, 50);
clearInterval(binaryConvert)
console.log("decimal clicked " + decimalActive)
})
binary.addEventListener('click', () =>{
//sets binaryConvert interval and clearing decimalConvert interval
binaryConvert = setInterval(convertBinary, 50)
clearInterval(decimalConvert)
console.log("binary clicked " + binaryActive)
})
//functions for event handlers
function convertDecimal() {
//convert decimals to other types
binary.value = Number(decimal.value).toString(2);
octal.value = Number(decimal.value).toString(8);
hex.value = Number(decimal.value).toString(16);
console.log("function convertDecimal executed")
if (decimal.value === "") {
binary.value = "";
octal.value = "";
hex.value = "";
}
}
function convertBinary() {
//converts binary to other types
decimal.value = parseInt(binary.value, 2);
octal.value = parseInt(binary.value, 2).toString(8);
hex.value = parseInt(binary.value, 2).toString(16);
console.log("function convertBinary executed")
if (binary.value === "") {
decimal.value = "";
octal.value = "";
hex.value = "";
}
}
你做对的是clearInterval
十进制。但是,您没有为二进制执行此操作。因此,您添加了第二个事件侦听器console.log
来检查您的工作,我使用它来查看哪些时间间隔已执行和未执行以解决此问题。
推荐阅读
- html - 在输入标签中键入内容后是否会触发任何 CSS 选择器?
- c# - 如何在检查员统一中显示“对象”变量
- linux - 结合Netstat和PS的输出 - linux
- node.js - 由代码创建的heroku文件在哪里?
- python - 使用 PyCharm 调试器运行 Flask CLI 命令
- javascript - 从路线 HERE 地图中排除特定道路
- android - 是否可以使用云功能为具有不同 Firebase 项目的 2 个不同应用程序发送 FCM 通知
- php - 忽略值 $request == null
- java - 从最近刷入应用程序后,CountDownTimer 未在服务中运行?
- vba - VBA 不会将 vba 参考转换为 excel 参考