第一个计算器上的减法代码

时间:2017-03-08 04:30:23

标签: javascript calculator

首先,您好,我是这个网站的新手,我也很新,还在学习。作为一个小型初学者项目,我一直在研究计算器。我在减法部分遇到了一些问题。我会发布我的全部工作,也许可能会出现一些想法。谢谢! //如果有人想知道,我正在使用Brackets

<html>
<head>
</head>
<body>
<form name="frm">

<input name="result">
</br>
<input type="button" name="1" value="1" onClick="run1()">
<input type="button" name="1" value="2" onClick="run2()">
<input type="button" name="1" value="3" onClick="run3()">
<input type="button" name="1" value="4" onClick="run4()">
<input type="button" name="1" value="5" onClick="run5()">
<input type="button" name="1" value="6" onClick="run6()">
<input type="button" name="1" value="7" onClick="run7()">
<input type="button" name="1" value="8" onClick="run8()">
<input type="button" name="1" value="9" onClick="run9()">
<input type="button" name="plus" value="+" onClick="runplus()">
</br>
<input type="button" name="calc" value="=" onClick="evalu()">
</form>
<script type="text/JavaScript">

function run1()
{document.frm.result.value += "1";}
function run2()
{document.frm.result.value += "2";}
function run3()
{document.frm.result.value += "3";}
function run4()
{document.frm.result.value += "4";}
function run5()
{document.frm.result.value += "5";}
function run6()
{document.frm.result.value += "6";}
function run7()
{document.frm.result.value += "7";}
function run8()
{document.frm.result.value += "8";}
function run9()
{document.frm.result.value += "9";}
function runplus()
{document.frm.result.value += "+";}

function evalu(){

 var evalo = eval(document.frm.result.value)

 document.frm.result.value = evalo;

}


</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

这实际上并不是构建计算器的最佳或有效方式;然而,既然你是全新的,我们现在可以把它推到一边。欢迎使用JavaScript! :d

您应该能够创建一个减号按钮,就像您创建加号按钮一样。

<input type="button" value="-" onClick="runminus()">

使用Javascript:

function runminus() {
    document.frm.result.value += "-";
}