gpt4 book ai didi

Javascript - 帮助更新我的计算器的显示以显示按下按钮的数量

转载 作者:行者123 更新时间:2023-12-02 21:07:51 25 4
gpt4 key购买 nike

我正在使用 HTML、CSS 和 Javascript 作为项目构建一个计算器。我已经设置好了 HTML 和 CSS - 我在编写 javascript 时遇到了麻烦,这样我就可以与 HTML 元素进行交互。我向数字按钮添加了事件监听器 - 单击时我希望它们更新显示屏上的文本。经过一段时间的研究,我无法完全弄清楚这样做的正确方法。

我尝试过通过按钮上的事件监听器更新显示的各种变体,并且无论按下哪个数字,display.textContent = button.innerText。但这不会更新显示屏上的整数。任何朝着正确方向的帮助都会有所帮助!谢谢!

HTML 位于下方,JavaScript 位于下方。

<body>
<div class = 'calculator-grid'>
<div class = 'output'>
<div class = 'previous-operand'></div>
<div data-display class = 'display'></div>
</div>
<button data-all-clear class = 'span-two grey-button'>AC</button>
<button data-delete class = 'grey-button'>DEL</button>
<button data-operator class = 'orange-button'>÷</button>
<button data-number >7</button>
<button data-number>8</button>
<button data-number>9</button>
<button data-operator class = 'orange-button'>*</button>
<button data-number>4</button>
<button data-number>5</button>
<button data-number>6</button>
<button data-operator class = 'orange-button'>+</button>
<button data-number>1</button>
<button data-number>2</button>
<button data-number>3</button>
<button data-operator class = 'orange-button'>-</button>
<button data-number class = 'span-two'>0</button>
<button data-number>.</button>
<button data-equals class = 'orange-button'>=</button>
</div>
const operatorButtons = document.querySelectorAll('[data-operator]');
const clearButton = document.querySelector('[data-all-clear]');
const deleteButton = document.querySelector('[data-delete]');
const equalButton = document.querySelector('[data-equals]');
const display = document.querySelector('[data-display]');

function updateDisplay() {

}

function clear() {
display.textContent = '';
}

clearButton.addEventListener('click', () => {
clear();
})


numberButtons.forEach(button => button.addEventListener('click',() => {
let buttonContent = button.innerText;
display.textContent = buttonContent;
}))

这是我的计算函数,我将根据我按下的数字调用它。

function add(a, b) {
return a + b;
}

function subtract(a, b) {
return a - b;
}

function multiply(a , b) {
return a * b;
}

function divide(a, b) {
return a / b;
}

function operate(firstNum, secondNum, operator) {
let result = 0;
let operationResult;
switch (operator) {
case '+':
operationResult = add(firstNum, secondNum);
result += operationResult;
break;
case '-':
operationResult = subtract(firstNum, secondNum);
result += operationResult;
break;
case '*':
operationResult = multiply(firstNum, secondNum);
result += operationResult;
break;
case '÷':
operationResult = divide(firstNum, secondNum);
result += operationResult;
break;
}
return result;
}

最佳答案

您可能忘记定义 numberButtons 了吗?

您的代码显示 numberButtons 尚未定义,在查询SelectorAll数字按钮并将它们定义为numberButton之后,您的代码应该可以工作。请参阅我的代码片段。

const operatorButtons = document.querySelectorAll('[data-operator]');
const clearButton = document.querySelector('[data-all-clear]');
const deleteButton = document.querySelector('[data-delete]');
const equalButton = document.querySelector('[data-equals]');
const display = document.querySelector('[data-display]');
const numberButtons = document.querySelectorAll('[data-number]')

function updateDisplay() {

}

function clear() {
display.textContent = '';
}

clearButton.addEventListener('click', () => {
clear();
})




numberButtons.forEach(button => button.addEventListener('click',() => {
let buttonContent = button.innerText;
display.textContent = buttonContent;
}))
<div class = 'calculator-grid'>
<div class = 'output'>
<div class = 'previous-operand'></div>
<div data-display class = 'display'></div>
</div>
<button data-all-clear class = 'span-two grey-button'>AC</button>
<button data-delete class = 'grey-button'>DEL</button>
<button data-operator class = 'orange-button'>÷</button>
<button data-number >7</button>
<button data-number>8</button>
<button data-number>9</button>
<button data-operator class = 'orange-button'>*</button>
<button data-number>4</button>
<button data-number>5</button>
<button data-number>6</button>
<button data-operator class = 'orange-button'>+</button>
<button data-number>1</button>
<button data-number>2</button>
<button data-number>3</button>
<button data-operator class = 'orange-button'>-</button>
<button data-number class = 'span-two'>0</button>
<button data-number>.</button>
<button data-equals class = 'orange-button'>=</button>
</div>

关于Javascript - 帮助更新我的计算器的显示以显示按下按钮的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61180034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com