计算器示例/ JS和HTML元素之间的链接

问题描述 投票:0回答:1

我正在做一个计算器的练习示例,但我听不懂以下部分:

这是它的外观

const buttons = document.querySelectorAll(".btn");
const display = document.querySelector("#display");

 for (let i = 0; i < buttons.length; i++) {
   buttons[i].addEventListener("click", function () {
     let number = buttons[i].getAttribute("data-num");
    display.value += number;
   });
 }
    <section>
      <form>
        <input type="text" id="display" />
      </form>
      <div class="btns">
        <button type="button" class="btn" data-num="+">+</button>
        <button type="button" class="btn" data-num="-">-</button>
        <button type="button" class="btn" data-num="/">/</button>
        <button type="button" class="btn" data-num="*">*</button>
        <button type="button" class="btn" data-num="0">0</button>
        <button type="button" class="btn" data-num="1">1</button>
        <button type="button" class="btn" data-num="2">2</button>
        <button type="button" class="btn" data-num="3">3</button>
        <button type="button" class="btn" data-num="4">4</button>
        <button type="button" class="btn" data-num="5">5</button>
        <button type="button" class="btn" data-num="6">6</button>
        <button type="button" class="btn" data-num="7">7</button>
        <button type="button" class="btn" data-num="8">8</button>
        <button type="button" class="btn" data-num="9">9</button>

        <button type="button" class="e-btn" data-num="=">=</button>
        <button type="button" class="c-btn" data-num="C">C</button>
      </div>
    </section>

我想我了解for循环以及所有这些背后的想法。我假设JS中的const“ buttons >>”变成了array(?),

我想我明白了,但是由于没有HTML或单独的ID,因此如何将其链接到class中的单个按钮

array仅为JS创建](?),在HTML中到处都是button

例如,如何链接到表示buttons中的按钮“ 0

”的按钮的eventlistener [5] HTML

我正在做一个计算器的练习示例,但我听不懂以下部分:这看起来像是const button = document.querySelectorAll(“。btn”); const display = document ....

calculator
1个回答
0
投票

[有一个古老的说法,“它一直是计算器。”没有人知道它的来源,但是它对物理世界的洞察力令人惊讶。例如,在这里您正在计算计算器的元素,其可见元素是由Web浏览器计算的。该网络浏览器运行在通过操作系统内的计算而编译的代码上,而该代码是使用CPU本身通过计算器进行的另一次计算所编译的,CPU本身是一个计算器,代表Calculator Please Use。所有这些都是由人脑设计的,这是除了可以运行Doom的TI-89以外的最高形式的计算器。

© www.soinside.com 2019 - 2024. All rights reserved.