gpt4 book ai didi

javascript - 纯 Javascript : What is the best way to click a button in the group of buttons in a calculator?

转载 作者:行者123 更新时间:2023-12-01 16:09:47 26 4
gpt4 key购买 nike

我有这段代码,是我用基于 Mac/Apple 计算器的 HTML/CSS 构建的。它的显示方式是使用 flexbox 将按钮分组为 5 行。

这是我的代码 codepen :

<div class="wrapper">
<div class="calheader">
<h2>Simple Calculator</h2>
</div>
<div class="calculatorbox">
<div class="calwindow">
<!-- ENTRY BOX -->
<div class="entry">
<p id="answer"></p>
</div>
<div class="entryhistory">
<p id="history"></p>
</div>
</div>
<!-- BUTTONS \\-->
<div class="calbuttons">
<div class="row">
<button id="clear" class="key topcolor" value="clear">C</button>
<button class="key topcolor" value="plusminus"><sup>+</sup>/<sub>−</sub></button>
<button class="key topcolor" value="%">%</button>
<button id="divide" class="key orange" value="/">÷</button>
</div>
<div class="row">
<button id="seven" class="key" value="7">7</button>
<button id="eight" class="key" value="8">8</button>
<button id="nine" class="key" value="9">9</button>
<button id="multiply" class="key orange" value="*">×</button>
</div>
<div class="row">
<button id="four" class="key" value="4">4</button>
<button id="five" class="key" value="5">5</button>
<button id="six" class="key" value="6">6</button>
<button id="subtract" class="key orange" value="-">−</button>
</div>
<div class="row">
<button id="one" class="key" value="1">1</button>
<button id="two" class="key" value="2">2</button>
<button id="three" class="key" value="3">3</button>
<button id="add" class="key orange" value="+">+</button>
</div>
<div class="row">
<button id="zero" class="key btnspan" value="0">0</button>
<button id="decimal" class="key" value=".">.</button>
<button id="equals" class="key orange" value="=">=</button>
</div>
</div>
</div>
</div>

现在,我想弄清楚如何选择按钮,以便我可以对每个按钮使用 addEventListener

我查过的其他纯javascript教程只有这样显示:

<div class="calculator-keys">

<button type="button" class="operator" value="+">+</button>
<button type="button" class="operator" value="-">-</button>
<button type="button" class="operator" value="*">&times;</button>
<button type="button" class="operator" value="/">&divide;</button>

<button type="button" value="7">7</button>
<button type="button" value="8">8</button>
<button type="button" value="9">9</button>


<button type="button" value="4">4</button>
<button type="button" value="5">5</button>
<button type="button" value="6">6</button>


<button type="button" value="1">1</button>
<button type="button" value="2">2</button>
<button type="button" value="3">3</button>


<button type="button" value="0">0</button>
<button type="button" class="decimal" value=".">.</button>
<button type="button" class="all-clear" value="all-clear">AC</button>

<button type="button" class="equal-sign operator" value="=">=</button>

</div>

这样他就可以使用这段代码了:

const keys = document.querySelector('.calculator-keys');

keys.addEventListener('click', (event) => {
const { target } = event;
console.log('digit', target.value);
});

据我了解,我检查过的本教程使用 querySelector 选择类 calculator-keys 中的所有子项。

在我的例子中,我能够让它只对第一行起作用。但是,如果我使用 querySelectorAll,我是否需要使用 .map().forEach() 或其他东西才能使用addEventListener 在每个按钮上?

最佳答案

querySelector 只返回一个元素,所以你需要使用 querySelectorAll您需要选择具有 key 类的所有元素,然后为每个键添加事件监听器

 const keys = document.querySelectorAll('.key');

keys.forEach(item => {
item.addEventListener('click', (event) => {
const { target } = event;
console.log('digit', target.value);
});
});
<div class="wrapper">
<div class="calheader">
<h2>Simple Calculator</h2>
</div>
<div class="calculatorbox">
<div class="calwindow">
<!-- ENTRY BOX -->
<div class="entry">
<p id="answer"></p>
</div>
<div class="entryhistory">
<p id="history"></p>
</div>
</div>
<!-- BUTTONS \\-->
<div class="calbuttons">
<div class="row">
<button id="clear" class="key topcolor" value="clear">C</button>
<button class="key topcolor" value="plusminus"><sup>+</sup>/<sub>−</sub></button>
<button class="key topcolor" value="%">%</button>
<button id="divide" class="key orange" value="/">÷</button>
</div>
<div class="row">
<button id="seven" class="key" value="7">7</button>
<button id="eight" class="key" value="8">8</button>
<button id="nine" class="key" value="9">9</button>
<button id="multiply" class="key orange" value="*">×</button>
</div>
<div class="row">
<button id="four" class="key" value="4">4</button>
<button id="five" class="key" value="5">5</button>
<button id="six" class="key" value="6">6</button>
<button id="subtract" class="key orange" value="-">−</button>
</div>
<div class="row">
<button id="one" class="key" value="1">1</button>
<button id="two" class="key" value="2">2</button>
<button id="three" class="key" value="3">3</button>
<button id="add" class="key orange" value="+">+</button>
</div>
<div class="row">
<button id="zero" class="key btnspan" value="0">0</button>
<button id="decimal" class="key" value=".">.</button>
<button id="equals" class="key orange" value="=">=</button>
</div>
</div>
</div>
</div>

关于javascript - 纯 Javascript : What is the best way to click a button in the group of buttons in a calculator?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63370645/

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