给出答案后,Javascript 计算器将无法清除

Javascript Calculator will not clear after answer is given

本文关键字:清除 计算器 Javascript 答案      更新时间:2023-09-26

当我在浏览器中打开它时,我可以输入我想要的方程式,但是在显示答案后,我无法重置计算器。在我得到NaN之后,我被卡住了。任何帮助将不胜感激。谢谢

<!DOCTYPE html>
<HTML>
<HEAD>
    <script type = "text/javascript">
        function divide()
        {
            document.calculator.output.value += "/";
        }
        function add()
        {
            document.calculator.output.value += "+";
        }
        function num9()
        {
            document.calculator.output.value += "9";
        }
        function num8()
        {
            document.calculator.output.value += "8";
        }
        function num7()
        {
            document.calculator.output.value += "7";
        }
        function multiply()
        {
            document.calculator.output.value += "*";
        }
        function percent()
        {
            document.calculator.output.value += "%";
        }
        function num6()
        {
            document.calculator.output.value += "6";
        }
        function num5()
        {
            document.calculator.output.value += "5";
        }
        function num4()
        {
            document.calculator.output.value += "4";
        }

        function subtr()
        {
            document.calculator.output.value += "-";
        }
        function num3()
        {
            document.calculator.output.value += "3";
        }
        function num2()
        {
            document.calculator.output.value += "2";
        }
        function num1()
        {
            document.calculator.output.value += "1";
        }
        function numZero()
        {
            document.calculator.output.value += "0";
        }
        function buildFormula()
        {
            var evalu= alert(eval(document.calculator.output.value)) + document.clear();
            document.calculator.output.value= evalu;
        }

    </script>
</HEAD>
<BODY>
<form name="calculator">
<table border=0>
<tr>
    <td colspan=4><input type=text readonly=true name="output" size = 25></td>
</tr>
<tr>
    <td><input type=button value= "   7   "  onClick="num7()"></td>
    <td><input type=button value= "   8   " onClick="num8()"></td>
    <td><input type=button value= "   9   " onClick="num9()"></td>
    <td><input type=button value= "    /  " onClick="divide()"></td>
    <td><input type=button value= "     + " onClick="add()"></td>
</tr>
<tr>
    <td><input type=button value= "   4   " onClick="num4()"></td>
    <td><input type=button value= "   5 " onClick="num5()"></td>
    <td><input type=button value= "   6  " onClick= "num6()"></td>
    <td><input type=button value= "    *   " onClick= "multiply()"></td>
    <td><input type=button value= "    %    " onClick="percent()"></td>
</tr>
    <tr>
        <td><input type=button value = "   1   " onClick="num1()"></td>
        <td><input type=button value = "   2   " onClick="num2()"></td>
        <td><input type=button value = "   3   " onClick="num3()"></td>
        <td><input type=button value = "    -    " onClick="subtr()"></td>
        <td><input type=button value = "     =     " onClick="buildFormula()"></td>
</tr>
<tr>
    <td colspan=5><input type=button value = "             0             " onClick="numZero()"></td>
</tr>
</table>
</form>
</BODY>
</HTML>

您的问题,因为您正在尝试将alert的返回与document.clear()的返回汇总 - 这是错误的,alert 不返回任何内容,并且 document.clear 是未定义的方法

将你的函数更改为这个:

 function buildFormula()
    {
        var evalu= eval(document.calculator.output.value);
        document.calculator.output.value= '';
        alert(evalu);
    }