<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script>
window.onload = function () {
var value1 = document.getElementById("value1");
var value2 = document.getElementById("value2");
var value3 = document.getElementById("value3");
var btn = document.getElementById("btn");
var select = document.getElementById("select")
btn.onclick = function () {
var one, two, bignum; try {
one = value1.value.split(".")[1].length } catch (e) { one = 1 } try {
two = value2.value.split(".")[1].length } catch (e) { two = 1 }
bignum = Math.pow(10, Math.max(one, two))
if (value1.value == "" || value2.value == "") {
alert('值不能为空') value1.value = ""; value2.value = ""; value3.value = ""; }
else if ((value1.value != parseFloat(value1.value)) || (value2.value != parseFloat(value2.value))) {
alert("输入的值有误") value1.value = ""; value2.value = ""; value3.value = ""; }
else { if (select.value == "+") { value = (parseFloat(value1.value) * bignum + parseFloat(value2.value) * bignum) / bignum value3.value = value }
else if (select.value == "-") { value = (parseFloat(value1.value) * bignum - parseFloat(value2.value) * bignum) / bignum value3.value = value }
else if (select.value == "*") { value = (parseFloat(value1.value) * bignum * parseFloat(value2.value) * bignum) / (bignum * bignum) value3.value = value }
else { if (parseFloat(value2.value) == 0) { alert("除数不能为零") value1.value = ""; value2.value = ""; value3.value = ""; }
else { value = ((parseFloat(value1.value) * bignum) / (parseFloat(value2.value) * bignum)) value3.value = value
}
}
}
}
}
</script>
</head>
<body>
<div class="box">
<h1>简易计算</h1>
<input type="text" id="value1">
<select id="select">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option> </select>
<input type="text" id="value2">
<input type="button" id="btn" value="=">
<input type="text" id="value3">
</div>
</body>
</html>