简体   繁体   English

计算器:修复删除按钮

[英]Calculator: fix delete button

I am unable to understand how to code the javascript for the button which delete the recent character from display of the calculator.我无法理解如何为从计算器显示中删除最近字符的按钮编写 javascript 代码。

When I try to do by this way length - 1 it deletes all my values written in display of the calculator and displays me random number or undefined .当我尝试通过这种方式进行时, length - 1它会删除我在计算器显示中写入的所有值并显示我随机数或undefined

 let string = ''; // let Delete = document.getElementsByClassName('del'); let screen = document.getElementById('screen'); let buttons = document.querySelectorAll('.button'); function del() { screen.value = screen.value.length - 1; } Array.from(buttons).forEach((button) => { button.addEventListener('click', (e) => { if (e.target.innerHTML == '=') { string = eval(string); document.querySelector('input').value = string; } else if (e.target.innerHTML == 'C') { string = ""; document.querySelector('input').value = string; } else if (e.target.innerHTML == 'Del') { string = del(); document.querySelector('input').value = string; } else { console.log(e.target) string = string + e.target.innerHTML; document.querySelector('input').value = string; } }) })
 <style>@import url('https://fonts.googleapis.com/css2?family=Macondo&display=swap'); body { font-family: 'Macondo', cursive; border: 1px solid rgb(220, 255, 48); border-radius: 5px; width: 500px; height: 600px; margin-left: 500px; margin-top: 53px; overflow: hidden; } .text-h { text-align: center; text-decoration: underline; border: 2px solid rgb(141, 237, 52); background: rgb(54, 254, 54); border-radius: 5px; padding: 25px; margin: -2px; } .button, .del { padding: 18px; margin: 0 5px; border: 2px solid black; border-radius: 40%; cursor: pointer; font-size: 20px; background-color: rgb(252, 255, 89); box-shadow: 5px rgb(126, 122, 122); } .flex { display: flex; justify-content: center; align-items: center; } .flex-col { flex-direction: column; } /* .bg-red{ */ /* background-color: red; */ /* } */ .row { margin: 8px 0px; } .row input { padding: 15px 9px; border: 2px solid black; border-radius: 17px; font-size: 19px; background-color: rgb(205, 255, 145); } #last { margin-left: 13px; } #first-row { font-size: 1px; width: 285px; justify-content: center; display: flex; margin-top: 9px; margin-bottom: 5px; } .first { margin: auto; } #M+, #M- { width: 54px; } button #m, #c-2, #del, #c-4, #add, #div, #mult { background-color: rgb(255, 188, 2); } #eval { width: 122px; border-radius: 30px; background-color: rgb(53, 137, 255); } #C { background-color: rgb(255, 42, 0); } #mult { width: 53px; } #div { width: 51px; } .container { background-color: rgb(121, 255, 100); padding: 24px; } @media screen and (max-width: 400px) { body { width: 324px; margin: auto; margin-top: 17px; overflow: scroll; } } </style>
 <h1 class="text-h">Welcome To My Calculator !</h1> <div class="container ma mw-20 flex flex-col bg-red"> <div class="row"> <input id="screen" class="input" type="text" /> </div> <div id="first-row" class="row first"> <button id="C" class="button">C</button> <button id="c-2" class="button">%</button> <button id="del" class="button">Del</button> <button id="c-4" class="button">-</button> </div> <div class="row"> <button class="button">7</button> <button class="button">8</button> <button class="button">9</button> <button id="mult" class="button">*</button> </div> <div class="row"> <button class="button">4</button> <button class="button">5</button> <button class="button">6</button> <button id="div" class="button">/</button> </div> <div class="row"> <button class="button">1</button> <button class="button">2</button> <button class="button">3</button> <button id="add" class="button">+</button> </div> <div class="row right"> <button class="button">0</button> <button class="button">.</button> <button id="eval" class="button">=</button> </div> </div>

You can use string = string.slice(0, -1);您可以使用string = string.slice(0, -1); for it.为了它。 It will delete last character that is entered.它将删除输入的最后一个字符。

That way you don't even need del() method.这样你甚至不需要del()方法。

See the code below:请看下面的代码:

 let string = ''; // let Delete = document.getElementsByClassName('del'); let screen = document.getElementById('screen'); let buttons = document.querySelectorAll('.button'); Array.from(buttons).forEach((button) => { button.addEventListener('click', (e) => { if (e.target.innerHTML == '=') { string = eval(string); document.querySelector('input').value = string; } else if (e.target.innerHTML == 'C') { string = ""; document.querySelector('input').value = string; } else if (e.target.innerHTML == 'Del') { string = string.slice(0, -1); document.querySelector('input').value = string; } else { console.log(e.target) string = string + e.target.innerHTML; document.querySelector('input').value = string; } }) })
 <style>@import url('https://fonts.googleapis.com/css2?family=Macondo&display=swap'); body { font-family: 'Macondo', cursive; border: 1px solid rgb(220, 255, 48); border-radius: 5px; width: 500px; height: 600px; margin-left: 500px; margin-top: 53px; overflow: hidden; } .text-h { text-align: center; text-decoration: underline; border: 2px solid rgb(141, 237, 52); background: rgb(54, 254, 54); border-radius: 5px; padding: 25px; margin: -2px; } .button, .del { padding: 18px; margin: 0 5px; border: 2px solid black; border-radius: 40%; cursor: pointer; font-size: 20px; background-color: rgb(252, 255, 89); box-shadow: 5px rgb(126, 122, 122); } .flex { display: flex; justify-content: center; align-items: center; } .flex-col { flex-direction: column; } /* .bg-red{ */ /* background-color: red; */ /* } */ .row { margin: 8px 0px; } .row input { padding: 15px 9px; border: 2px solid black; border-radius: 17px; font-size: 19px; background-color: rgb(205, 255, 145); } #last { margin-left: 13px; } #first-row { font-size: 1px; width: 285px; justify-content: center; display: flex; margin-top: 9px; margin-bottom: 5px; } .first { margin: auto; } #M+, #M- { width: 54px; } button #m, #c-2, #del, #c-4, #add, #div, #mult { background-color: rgb(255, 188, 2); } #eval { width: 122px; border-radius: 30px; background-color: rgb(53, 137, 255); } #C { background-color: rgb(255, 42, 0); } #mult { width: 53px; } #div { width: 51px; } .container { background-color: rgb(121, 255, 100); padding: 24px; } @media screen and (max-width: 400px) { body { width: 324px; margin: auto; margin-top: 17px; overflow: scroll; } } </style>
 <h1 class="text-h">Welcome To My Calculator !</h1> <div class="container ma mw-20 flex flex-col bg-red"> <div class="row"> <input id="screen" class="input" type="text" /> </div> <div id="first-row" class="row first"> <button id="C" class="button">C</button> <button id="c-2" class="button">%</button> <button id="del" class="button">Del</button> <button id="c-4" class="button">-</button> </div> <div class="row"> <button class="button">7</button> <button class="button">8</button> <button class="button">9</button> <button id="mult" class="button">*</button> </div> <div class="row"> <button class="button">4</button> <button class="button">5</button> <button class="button">6</button> <button id="div" class="button">/</button> </div> <div class="row"> <button class="button">1</button> <button class="button">2</button> <button class="button">3</button> <button id="add" class="button">+</button> </div> <div class="row right"> <button class="button">0</button> <button class="button">.</button> <button id="eval" class="button">=</button> </div> </div>

function del(){
    screen.value = screen.value.length - 1;
}

The code inside the above function del() is just decrementing the length of the string by 1.上述函数 del() 中的代码只是将字符串的长度减 1。

What is supposed to be done here is slicing the string by removing the last character from the string whenever del button is clicked.这里应该做的是在单击 del 按钮时通过从字符串中删除最后一个字符来对字符串进行切片。

And the reason of displaying 'undefined' is that the del() function is not returning any value to the function call.显示“未定义”的原因是 del() 函数没有向函数调用返回任何值。

So the below code helps while using del() function:因此,以下代码在使用 del() 函数时会有所帮助:

function del(){
    screen.value = screen.value.slice(0, -1);
    console.log(`Screen Value : ${screen.value}`)
    return screen.value;
}

But we do not need del() function as well.但是我们也不需要 del() 函数。

string = string.slice(0, -1);
document.querySelector("input").value = string;

The above lines can do our work.以上几行可以完成我们的工作。

Please refer to the complete script below.请参阅下面的完整脚本。

    let string = "";
    let buttons = document.querySelectorAll(".button");
    
    Array.from(buttons).forEach((button) => {
        button.addEventListener("click", (e) => {
            if (e.target.innerHTML == "=") {
                string = eval(string);
                document.querySelector("input").value = string;
            } else if (e.target.innerHTML == "C") {
                string = "";
                document.querySelector("input").value = string;
            } else if (e.target.innerHTML == "Del") {
                string = string.slice(0, -1);
                document.querySelector("input").value = string;
            } else {
                console.log(e.target);
                string = string + e.target.innerHTML;
                document.querySelector("input").value = string;
            }
        });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM