如何在HTML&中实现基本计算器使用Javascript

时间:2017-11-18 11:02:47

标签: javascript html css

我正在尝试编写一个html代码来添加,减去,除以或乘以两个或更多数字,但我遇到了困难...(我尝试添加一些CSS只是为了让它看起来不错,但我是更关心首先运行的代码)。我也在使用Javascript。我做错了什么?

<html>
<head>

<body bgcolor="FFFCC">
<table border="0" cellpadding="0"
cellspacing="1" sytyle ="border-collapse. collapse"
bordercolor="#1111" width ="50%">

<hi><p align="center">CALCULATOR</p></hi><br>
<script language= "javascript">
function ADD()
{
var first=parsefloat(txtNumber1.value);
var second=parsefloat(txtNumber2.value);
var Ans =first+second;
txtAnswer.value=Ans;
}

function MINUS()
{
var first=parsefloat(txtNumber1.value);
var second=parsefloat(txtNumber2.value);
var Ans =first-second;
txtAnswer.value=Ans;

function DIVIDE()
{
var first=parsefloat(txtNumber1.value);
var second=parsefloat(txtNumber2.value);
var Ans =first/second;
txtAnswer.value=Ans;
}

function MULTIPLY()
{
var first=parsefloat(txtNumber1.value);
var second=parsefloat(txtNumber2.value);
var Ans =first*second;
txtAnswer.value=Ans;
}
function MODULO()
{
var first=parsefloat(txtNumber1.value);
var second=parsefloat(txtNumber2.value);
var Ans =first%second;
txtAnswer.value=Ans;
}

</script> 
</head>



<input type ="text" name="txtNumber1"/><br>
<input type ="text" name="txtNumber2"/><br>
<input type ="button" name="butAnswer" 
value="+" onclick="ADD()"/>
<input type ="button" name="butAnswer"
value="-" onclick="MINUS()"/>
<input type ="button" name="butAnswer"
value="/" onclick="DIVIDE()"/>
<input type ="button" name="butAnswer"
value="*" onclick="MULTIPLY()"/>
<input type ="button" name="butAnswer"
value="%" onclick="MODULO()"/><br>
<input type ="text" name="txtAnswer"/>

</table>
</body>
</html>

3 个答案:

答案 0 :(得分:1)

尝试纠正这些行:

var first=parsefloat(document.getElementsByName("txtNumber1")[0].value);
var second=parsefloat(document.getElementsByName("txtNumber2")[0].value);

document.getElementsByName("txtAnswer")[0].value=Ans;

答案 1 :(得分:1)

您没有引用输入元素!

在这个例子中

var first=parsefloat(txtNumber1.value);

txtNumber1未定义

这将有效

var first=parsefloat(document.getElementsByName(txtNumber1)[0].value);

答案 2 :(得分:1)

  <html>
<head>

</head>
<body bgcolor="FFFCC">

  <table width="50%" align="center">
    <tr><td colspan="2"><hi><p>CALCULATOR</p></hi></td></tr>
    <tr><td><input type ="text" id="txtNumber1" placeholder="Enter first number"/><input type ="text" id="txtNumber2" placeholder="Enter second number"/> <input type ="text" id="txtAnswer"/></td>
<tr><td><input type ="button" name="butAnswer" 
value="+" onclick="ADD()"/>
<input type ="button" name="butAnswer"
value="-" onclick="MINUS()"/>
<input type ="button" name="butAnswer"
value="/" onclick="DIVIDE()"/>
<input type ="button" name="butAnswer"
value="*" onclick="MULTIPLY()"/>
<input type ="button" name="butAnswer"
       value="%" onclick="MODULO()"/></td></tr>
    <tr>

</table>

<script language= "javascript">
var answer=document.getElementById("txtAnswer")
function ADD()
{

  var number1=document.getElementById("txtNumber1")
  var number2=document.getElementById("txtNumber2")
    number1=parseFloat(number1.value)
    number2=parseFloat(number2.value)
var Ans =number1+number2;
answer.value=Ans;
}

function MINUS()
{

  var number1=document.getElementById("txtNumber1")
  var number2=document.getElementById("txtNumber2")
    number1=parseFloat(number1.value)
    number2=parseFloat(number2.value)
var Ans =number1-number2;
answer.value=Ans;
}
function DIVIDE()
{

  var number1=document.getElementById("txtNumber1")
  var number2=document.getElementById("txtNumber2")
    number1=parseFloat(number1.value)
    number2=parseFloat(number2.value)
var Ans =number1/number2;
answer.value=Ans;
}

function MULTIPLY()
{

  var number1=document.getElementById("txtNumber1")
  var number2=document.getElementById("txtNumber2")
    number1=parseFloat(number1.value)
    number2=parseFloat(number2.value)
var Ans =number1*number2;
answer.value=Ans;
}
function MODULO()
{

  var number1=document.getElementById("txtNumber1")
  var number2=document.getElementById("txtNumber2")
    number1=parseFloat(number1.value)
    number2=parseFloat(number2.value)
var Ans =number1%number2;
answer.value=Ans;
}   

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