【问题标题】:Calculator need to delete only one number计算器只需要删除一个数字
【发布时间】:2019-08-10 14:38:35
【问题描述】:

尝试制作一个简单的 javascript 计算器并遇到问题。 按

    function run1(){

    	document.case.display.value += "1"
    };
    function run2(){

    	document.case.display.value += "2"
    };
    function run3(){

    	document.case.display.value += "3"
    };
    function run4(){

    	document.case.display.value += "4"
    };
    function run5(){

    	document.case.display.value += "5"
    };

    function run6(){

    	document.case.display.value += "6"
    };

    function run7(){

    	document.case.display.value += "7"
    };

    function run8(){

    	document.case.display.value += "8"
    };

    function run9(){

    	document.case.display.value += "9"
    };

    function run0(){

    	document.case.display.value += "0"
    };
    function runPlus(){

    	document.case.display.value += "+"
    };
    function runMinus(){

    	document.case.display.value += "-"
    };
    function runDivide(){

    	document.case.display.value += "/"
    };
    function runMultiply(){

    	document.case.display.value += "*"
    };
    function runComma(){

    	document.case.display.value += "."
    };
    function runBack(){

    	document.case.display.value -= "2"
    };

    function runC(){

    	document.case.display.value = ""
    };


    function runEquals() {
            if (document.case.display.value == "") {
            	document.case.display.value = ""
            } else  {
    	var equals = eval(document.case.display.value)
    	document.case.display.value = equals;
    }
    }
    not (display) {
    	margin: 0;
    	padding: 0;
    	border: 0;
    	outline: 0;
    	font-size: 16px;
    	vertical-align: baseline;
    	background: transparent;
    }
    body {
    	line-height: 1;
    }
    ul {
    	list-style: none;
    }

    body {
    	width: 500px;
    }
    form {
    	background-color: 080808;
    	text-align: center;
    	padding: 7px;


    }
    #display {
    	width: 98%;
    	height: 30px;
    	text-align: right;
    	font-size: 1.5rem;
    }
    .digit {
    	font-size: 2rem;
    	background-color: 	gray;
    	height: 55px;
    	width: 20%;
    	border-radius: 5px;
    	display: inline-block;
    	padding: 5px;
    }
    .oper {
    	font-size: 2rem;
    	background-color:green ;
    	height: 55px;
    	width: 20%;
    	border-radius: 5px;
    	display: inline-block;
    	padding: 5px;
    }
    #clearMem {
    	background-color: red;
    }
    #equal {
    	background-color: yellow;
    	width: 82%;

    }
    <html>
    <head>
        <title>Calculator Project</title>
         <link rel="stylesheet" href="main.css" type="text/css">
            <script src="main.js" type="text/javascript"></script>
    </head>

    <body>


    <form name="case">
      <input name="display" id="display" value="">

        <input type="button" class="digit" value="1" onclick="run1()">
        <input type="button" class="digit" value="2" onclick="run2()">
        <input type="button" class="digit" value="3" onclick="run3()">
        <input type="button" id="plus" 	class="oper" 	value="+"  onclick="runPlus()">

        <input type="button" class="digit" value="4" onclick="run4()">
        <input type="button" class="digit" value="5" onclick="run5()">
        <input type="button" class="digit" value="6" onclick="run6()">
        <input type="button" id="minus" 	class="oper" 	value="-" onclick="runMinus()" >

        <input type="button" class="digit" value="7" onclick="run7()">
        <input type="button" class="digit" value="8" onclick="run8()">
        <input type="button" class="digit" value="9" onclick="run9()">
        <input type="button" id="divide" 	class="oper" 	value="/" onclick="runDivide()" >

        <input type="button" class="digit" value="0" onclick="run0()">
        <input type="button" id="comma" class="digit" value="." onclick="runComma()">
        <input type="button" id="clearMem" class="oper" value="CE" onclick="runC()">

        <input type="button" id="multiply" 	class="oper" 	value="*" onclick="runMultiply()">
        <input type="button" id="back" 	class="oper"	value="<" onclick="runBack()">
    	<input type="button" id="equal" 	class="oper"	value="=" onclick="runEquals()">




    </body>

也许我们可以拆分为 10 我们可能需要 Math.ceil() 69/10 = 6.9 = 6

6.99/10 = 0.699 = 它也是 0 或 1 我只需要删除一个数字。

【问题讨论】:

    标签: javascript html css calculator


    【解决方案1】:

    当您按下返回时,您想从输入中删除最后一个字符。类似于value.slice(0, -1)。然后,将该值分配回您的输入。

    var val = document.case.display.value.slice(0, -1);
    document.case.display.value = val;
    

    删除最后一个字符的其他方法:https://stackoverflow.com/a/952945/1253479

    更新代码sn-p:

        function run1(){
    
        	document.case.display.value += "1"
        };
        function run2(){
    
        	document.case.display.value += "2"
        };
        function run3(){
    
        	document.case.display.value += "3"
        };
        function run4(){
    
        	document.case.display.value += "4"
        };
        function run5(){
    
        	document.case.display.value += "5"
        };
    
        function run6(){
    
        	document.case.display.value += "6"
        };
    
        function run7(){
    
        	document.case.display.value += "7"
        };
    
        function run8(){
    
        	document.case.display.value += "8"
        };
    
        function run9(){
    
        	document.case.display.value += "9"
        };
    
        function run0(){
    
        	document.case.display.value += "0"
        };
        function runPlus(){
    
        	document.case.display.value += "+"
        };
        function runMinus(){
    
        	document.case.display.value += "-"
        };
        function runDivide(){
    
        	document.case.display.value += "/"
        };
        function runMultiply(){
    
        	document.case.display.value += "*"
        };
        function runComma(){
    
        	document.case.display.value += "."
        };
        function runBack(){
            var val = document.case.display.value.slice(0, -1);
            document.case.display.value = val;
        };
    
        function runC(){
    
        	document.case.display.value = ""
        };
    
    
        function runEquals() {
                if (document.case.display.value == "") {
                	document.case.display.value = ""
                } else  {
        	var equals = eval(document.case.display.value)
        	document.case.display.value = equals;
        }
        }
        not (display) {
        	margin: 0;
        	padding: 0;
        	border: 0;
        	outline: 0;
        	font-size: 16px;
        	vertical-align: baseline;
        	background: transparent;
        }
        body {
        	line-height: 1;
        }
        ul {
        	list-style: none;
        }
    
        body {
        	width: 500px;
        }
        form {
        	background-color: 080808;
        	text-align: center;
        	padding: 7px;
    
    
        }
        #display {
        	width: 98%;
        	height: 30px;
        	text-align: right;
        	font-size: 1.5rem;
        }
        .digit {
        	font-size: 2rem;
        	background-color: 	gray;
        	height: 55px;
        	width: 20%;
        	border-radius: 5px;
        	display: inline-block;
        	padding: 5px;
        }
        .oper {
        	font-size: 2rem;
        	background-color:green ;
        	height: 55px;
        	width: 20%;
        	border-radius: 5px;
        	display: inline-block;
        	padding: 5px;
        }
        #clearMem {
        	background-color: red;
        }
        #equal {
        	background-color: yellow;
        	width: 82%;
    
        }
        <html>
        <head>
            <title>Calculator Project</title>
             <link rel="stylesheet" href="main.css" type="text/css">
                <script src="main.js" type="text/javascript"></script>
        </head>
    
        <body>
    
    
        <form name="case">
          <input name="display" id="display" value="">
    
            <input type="button" class="digit" value="1" onclick="run1()">
            <input type="button" class="digit" value="2" onclick="run2()">
            <input type="button" class="digit" value="3" onclick="run3()">
            <input type="button" id="plus" 	class="oper" 	value="+"  onclick="runPlus()">
    
            <input type="button" class="digit" value="4" onclick="run4()">
            <input type="button" class="digit" value="5" onclick="run5()">
            <input type="button" class="digit" value="6" onclick="run6()">
            <input type="button" id="minus" 	class="oper" 	value="-" onclick="runMinus()" >
    
            <input type="button" class="digit" value="7" onclick="run7()">
            <input type="button" class="digit" value="8" onclick="run8()">
            <input type="button" class="digit" value="9" onclick="run9()">
            <input type="button" id="divide" 	class="oper" 	value="/" onclick="runDivide()" >
    
            <input type="button" class="digit" value="0" onclick="run0()">
            <input type="button" id="comma" class="digit" value="." onclick="runComma()">
            <input type="button" id="clearMem" class="oper" value="CE" onclick="runC()">
    
            <input type="button" id="multiply" 	class="oper" 	value="*" onclick="runMultiply()">
            <input type="button" id="back" 	class="oper"	value="<" onclick="runBack()">
        	<input type="button" id="equal" 	class="oper"	value="=" onclick="runEquals()">
    
    
    
    
        </body>

    【讨论】:

      猜你喜欢
      • 2022-01-26
      • 1970-01-01
      • 2022-11-20
      • 2021-11-23
      • 2020-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-08
      相关资源
      最近更新 更多