【发布时间】:2021-05-20 17:56:49
【问题描述】:
const numberButtons = document.querySelectorAll('[data-number]')
const operationButtons = document.querySelectorAll('[data-operation]')
const equalsButton = document.querySelector('[data-equals]')
const deleteButton = document.querySelector('[data-delete]')
const allClearButton = document.querySelector('[data-all-clear]')
const previosOperandandTextElement = document.querySelector('[data-previous-operand]')
const currentOperandandTextElement = document.querySelector('[data-current-operand]')
s
class Calculator {
constructor(previosOperandandTextElement, currentOperandandTextElement) {
this.previosOperandandTextElement = previosOperandandTextElement
this.currentOperandandTextElement = currentOperandandTextElement
this.clear()
}
clear() {
this.currentOperand = ''
this.previousOperand = ''
this.operation = undefined
}
delete() {
}
appendNumber(number) {
this.currentOperand = number
}
chooseOperation(operation) {
}
compute() {
}
updateDisplay() {
this.currentOperandandTextElement.innerText = this.currentOperand
}
}
const calculator = new Calculator(previosOperandandTextElement, currentOperandandTextElement)
numberButtons.forEach(button => {
button.addEventListener('click', () => {
calculator.appendNumber(button.innerText)
calculator.updateDisplay()
})
})
*, *::before, *::after {
box-sizing:border-box;
font-family: Gotham Rounded, sans-serif;
font-weight: normal;
}
body {
padding: 0;
margin: 0;
background: linear-gradient(to right, blue, green)
}
.calculator-grid {
display: grid;
justify-content: center;
align-content: center;
min-height: 100vh;
grid-template-columns: repeat(4, 100px);
grid-template-rows: minmax(120px, auto) repeat(5,100px);
}
.calculator-grid > button {
cursor: pointer;
font-size: 2rem;
border: 1px solid white;
outline: none;
background-color: rgba(255, 255, 255, .75);
}
.calculator-grid > button:hover {
background-color: rgba(255, 255, 255, .9);
}
.span-two {
grid-column: span 2;
}
.output {
grid-column: 1 / -1;
background-color: rgba(0, 0, 0, .75);
display: flex;
align-items: flex-end;
justify-content: space-between;
flex-direction: column;
padding: 10px;
word-wrap: break-word;
word-break: break-all;
}
.output .previous-operand {
color: rgba(255, 255, 255, .75);
font-size: 1rem;
}
.output .current-operand {
color: white;
font-size: 1rem;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="calculator-grid">
<div class="output">
<div data-previous-operand class="previous-operand"></div>
<div data-current-operand 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="script.js" defer></script>
</body>
</html>
我正在使用 HTML、CSS 和 JS 构建我的第一个 javascript 计算器项目。当我在实时服务器中打开它时,我的 JS 代码没有任何反应。当我运行此代码是 VS Code 时,我注意到输出控制台中有一个参考错误。我已经切换了我的脚本标签,但什么也没有。关于这里可能出现的问题有什么帮助吗?Here is an image of the console with the error
【问题讨论】:
-
能否提供代码给我们?然后我们了解可能会引发引用错误的位置和原因。
-
这是一个DOM(客户端)方法,不能在node中使用。