【发布时间】:2021-09-04 13:56:28
【问题描述】:
作为 Odin 项目的一部分,我目前正在开发 HTML 计算器。
我遇到了 .textContent 的奇怪行为:这是我的 JavaScript 代码 sn-p:
//---output is a HTML tag at which the input of the user should be entered-------------------------------
const output=document.querySelector('.Output');
//------I store the input (pressed keys) into an array
let input=[]
//-------------------------------------------------
//--------------keybord support--------------------
document.addEventListener('keydown', function(e) {
if (e.key != "+" || e.key !="-" || e.key !="*" || e.key !="/") {
let internalVariable = 0;
input.push(parseInt(e.key));
internalVariable=input.join('');
output.innerHTML=internalVariable;
}
if (e.key=="+") {
console.log(typeof e.key,input)**-> Test if condition works**
}
问题是:每当我按下 + 按钮时,我仍然会得到一个输出 (NaN),并且我会在我的输入数组中得到一个条目 (NaN),这是不应该发生的。
我错过了理解的 text.Content 吗?
【问题讨论】:
-
您询问
textContent,但您的代码没有使用它...
标签: javascript arrays dom