javascript - 试图获取等于按钮以在计算器中正常运行

标签 javascript jquery html css javascript-events

我正在开发计算器应用程序。除了一件事,这几乎完成了。这是我的代码笔的完整代码:http://codepen.io/freefora11/pen/pgNbXw?editors=101

我遇到的问题是单击“等于”按钮时。下面的第一个代码可以完美地将两个数字相加...

$("#equals").click(function(){
            total = total + parseFloat(dis.text()); 
            dis.text(total);

    });


现在,我试图根据下面单击的运算符进行乘法,减法,除法等操作,但是它无法正常运行。它与if语句有关。我不知道为什么它不起作用。有没有正确的方向提示或帮助?谢谢!

$("#equals").click(function(){
        if(operation =="+"){
            total = total + parseFloat(dis.text()); 
            dis.text(total);
        }
        if(operation == "-"){
            total = total - parseFloat(dis.text());
            dis.text(total);
        }
        if(operation == "X"){
            total = total * parseFloat(dis.text());
            dis.text(total);
        }if(operation == "÷"){
            total = total / parseFloat(dis.text());
            dis.text(total);
        }
    });

最佳答案

operation = "";删除$(".numbers").click(function(){
因为它将使操作变量为空白。

Working codepen

关于javascript - 试图获取等于按钮以在计算器中正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34540712/

相关文章:

javascript - 停止将 HTML href 连接到地址栏

javascript - 用Jquery,如何获取按钮绑定(bind)的功能

html - 每个设备中的不同 SVG 布局

javascript - div 改变时发生的事件

javascript - 网站性能测试 : How best to approximate computer performance?

javascript - 重命名没有 id 的按钮文本

jquery - Windows 调整大小时的 css 行为

javascript - 在另一个方法中等待方法结束

javascript - 关于javascript alert和sendRedirect的问题

Javascript 缓存选择器函数