TypeError:无法将属性'innertext'设置为null

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TypeError:无法将属性'innertext'设置为null相关的知识,希望对你有一定的参考价值。

我正在开发javascript计算器应用程序。当前,当按下按钮时,出现此错误:

未捕获的TypeError:无法设置innerText的属性'null'

这是我的代码:

class Calculator 
  constructor(previousOperandTextElement, currentOperandTextElement) 
    this.previousOperandTextElement = previousOperandTextElement;
    this.currentOperandTextElement = currentOperandTextElement;
    this.clear();
  
  clear() 
    this.currentOperand = '';
    this.previousOperand = '';
    this.operation = '';
  
  delete() 
  appendNumber(number) 
    this.currentOperand = this.currentOperand.toString() + number.toString();
  
  chooseOperation(operation) 
  computer() 
  updateDisplay() 
    this.currentOperandTextElement.innerText = this.currentOperand;
  

const numberButtons = document.querySelectorAll('[data-number]');
const operationButtons = document.querySelectorAll('[data-operation]');
const equalsButton = document.querySelector(['data-equals']);
const allClearButton = document.querySelector(['data-all-clear']);
const previousOperandTextElement = document.querySelector(['data-previousOperand']);
const currentOperandTextElement = document.querySelector(['data-currentOperand']);
const calculator = new Calculator(previousOperandTextElement, currentOperandTextElement)
numberButtons.forEach(button => 
  button.addEventListener('click', () => 
    calculator.appendNumber(button.innerText)
    calculator.updateDisplay()
  )
)
 <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">
 <title>Calculator</title>
 <link rel="stylesheet" href="css/style.css">
 </head>
  <body>
     <div class="calculator-grid">
    <div class="output">
        <div data-previousOperand class="previous-operand"></div>
        <div data-currentOperand class="current-operand"></div>
    </div>
    <button data-all-clear class="span-two">AC</button>
    <button data-delete>Del</button>
    <button data-operation>/</button>
    <button data-number>1</button>
    <button data-number>2</button>
    <button data-number>3</button>
    <button data-operation>*</button>
    <button data-number>4</button>
    <button data-number>5</button>
    <button data-number>6</button>
    <button data-operation>+</button>
    <button data-number>7</button>
    <button data-number>8</button>
    <button data-number>9</button>
    <button data-operation>-</button>
    <button data-number>.</button>
    <button data-number>0</button>
    <button data-equals class="span-two">=</button>
    </div>
    <script src="js/index.js"></script>
    </body>
    </html>
答案

使用数据属性时。您不应在名称中使用驼峰式大小写。您应该用破折号隔开每个地方。

因此使用<div data-previous-operand ...并查找querySelector('[data-previous-operand]')

以上是关于TypeError:无法将属性'innertext'设置为null的主要内容,如果未能解决你的问题,请参考以下文章

Highcharts-TypeError:无法读取null的'isResponsiveOptions'属性

渲染错误:“ TypeError:无法读取null的属性'PAY_TYPE'”

TypeError:无法读取React BeatifulDnD上未定义的属性'map'

TypeError:无法读取未定义的属性'spacing'

karma TypeError“无法读取未定义的属性'subscribe'”

TypeError:无法读取未定义的属性'map'| ReactJS