Javascript 计算器,点击加号后如何存储一个值,点击等号后如何求和?
我正在制作一个计算器,但在点击加号后,在存储数值时遇到了一个问题,然后选择另一个数字来点击等号并得到结果。请帮帮我!XPJavascript 计算器,点击加号后如何存储一个值,点击等号后如何求和?,javascript,html,dom,Javascript,Html,Dom,我正在制作一个计算器,但在点击加号后,在存储数值时遇到了一个问题,然后选择另一个数字来点击等号并得到结果。请帮帮我!XP let firstValue; let secondValue; document.querySelector('#addOperator').addEventListener('click', function() { if(document.querySelector('#addOperator')) { firstValue = document.querySe
let firstValue;
let secondValue;
document.querySelector('#addOperator').addEventListener('click', function() {
if(document.querySelector('#addOperator')) {
firstValue = document.querySelector('#output').textContent;
// document.querySelector('#output').textContent = '';
// console.log(firstValue);
console.log(document.querySelector('#output').textContent);
};
});
document.getElementById('equals').addEventListener('click', function() {
if(document.querySelector('#output').textContent === firstValue) {
secondValue = document.querySelector('#output').textContent;
// console.log(secondValue);
// document.querySelector('#output').textContent = add(firstValue, secondValue);
console.log(document.querySelector('#output').textContent = add(firstValue, secondValue));
};
});
我的html文档如下------------------------------------------>
<!-- Calculator display -->
<div id="output">
<!-- <div id="previous-operand"></div>
<div id="current-operand"></div> -->
</div>
<button class="span-two" id="ac-color">AC</button>
<button id="clear-entry">CE</button>
<button id="divideOperator">÷</button>
<button class="number">7</button>
<button class="number">8</button>
<button class="number">9</button>
<button id="multiplyOperator">*</button>
<button class="number">4</button>
<button class="number">5</button>
<button class="number">6</button>
<button id="addOperator">+</button>
<button class="number">1</button>
<button class="number">2</button>
<button class="number">3</button>
<button id="subtractOperator">-</button>
<button class="number">0</button>
<button class="number">.</button>
<button class="span-two" id="equals">=</button>
自动控制
总工程师
÷
7.
8.
9
*
4.
5.
6.
+
1.
2.
3.
-
0
.
=
如果用户单击数字按钮,则需要添加程序代码以捕获所按下按钮的值。仅仅在+和=按钮上添加程序代码是不够的。你可以这样说:@是的,我想我需要研究那些数据-*。谢谢你的帮助。