一个简单的计算器
javascript吧
全部回复
仅看楼主
level 1
jbweb2006 楼主
<!DOCTYPE html>
<html>
<head>
<title>简易计算器</title>
<meta charset="gb2312">
<style type="text/css">
#num1 { border: 1px solid red;}
#fh { border: 1px solid #ff00ff;}
#num2 { border: 1px solid green;}
#result { border: 1px solid blue;}
#btn1 { background: black; color:white;}
</style>
<script type="text/javascript">
function show()
{
var num1 = document.getElementById("num1").value;
var num2 = document.getElementById("num2").value;
var fh = document.getElementById("fh").value;
num1 = parseFloat(num1);
num2 = parseFloat(num2);
switch(fh)
{
case "+":
res = num1+num2;
break;
case "-":
res = num1-num2;
break;
case "*":
res = num1*num2;
break;
case "/":
res = num1/num2;
break;
default:
break;
}
document.getElementById("result").value = res;
}
</script>
</head>
<body>
第一个数:<input type="text" id="num1">
<br><br>
运算符号:
<select id="fh">
<option value="+">加</option>
<option value="-">减</option>
<option value="*">乘</option>
<option value="/">除</option>
</select>
<br><br>
第二个数:<input type="text" id="num2">
<br><br>
<input type="button" value="运算" onclick="show()" id="btn1">
<br><br>
结果:<input type="text" id="result">
</body>
</html>
2019年07月26日 14点07分 1
level 3
eval();
2019年07月28日 14点07分 3
level 1
jbweb2006 楼主
<!DOCTYPE html>
<html>
<head>
<title>另一个计算器</title>
<meta charset="gb2312">
<script>
var number = null;
var DataNum = null;
function calc(number)
{
DataNum = document.getElementById("datanumber");
if(number=="%")
{
DataNum.value = Math.round(DataNum.value)/100;
}
else
{
DataNum.value += document.getElementById(number).value;
}
}
function back()
{
DataNum.value = DataNum.value.substring(0,DataNum.value.length-1);
}
function eva()
{
DataNum.value = eval(DataNum.value);
}
function back2()
{
DataNum.value = "";
}
</script>
</head>
<body>
<input type="text" id="datanumber">
<br>
<input type="button" value="1" id="1" onclick="calc(this.id)">
<input type="button" value="2" id="2" onclick="calc(this.id)">
<input type="button" value="3" id="3" onclick="calc(this.id)">
<input type="button" value="+" id="+" onclick="calc(this.id)">
<br>
<input type="button" value="4" id="4" onclick="calc(this.id)">
<input type="button" value="5" id="5" onclick="calc(this.id)">
<input type="button" value="6" id="6" onclick="calc(this.id)">
<input type="button" value="-" id="-" onclick="calc(this.id)">
<br>
<input type="button" value="7" id="7" onclick="calc(this.id)">
<input type="button" value="8" id="8" onclick="calc(this.id)">
<input type="button" value="9" id="9" onclick="calc(this.id)">
<input type="button" value="*" id="*" onclick="calc(this.id)">
<br>
<input type="button" value="0" id="0" onclick="calc(this.id)">
<input type="button" value="/" id="/" onclick="calc(this.id)">
<input type="button" value="=" onclick="eva()">
<input type="button" value="退格" onclick="back()">
<input type="button" value="清零" onclick="back2()">
</body>
</html>
2019年08月12日 10点08分 4
1