帮我写一个网页计算器公式代码
发布网友
发布时间:2022-05-10 07:08
我来回答
共1个回答
热心网友
时间:2023-11-18 19:23
写好了,代码如下,天气好冷啊!
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
<style>
input{
width: 33px;
}
#compute{
width: 50px;
}
</style>
</head>
<body>
<div id="content">
(<input id="num1" type="text">*
<input id="num2" type="text">)/
<input id="num3" type="text">+0.01=
<input id="result" type="text">
<input type="button" id="compute" value="计算">
</div>
<script>
var n1 = document.getElementById('num1');
var n2 = document.getElementById('num2');
var n3 = document.getElementById('num3');
var rst = document.getElementById('result');
var com = document.getElementById("compute");
com.addEventListener("click",function(){
if(n1.value.length == 0 ||n2.value.length == 0 ||n3.value.length == 0){
alert("请填写完毕再进行计算!");
return false;
}
if(n3.value == 0){
alert("除数不能为 0!");
return false;
}
var result = (n1.value*n2.value)/n3.value+0.01;
rst.value = result;
})
</script>
</body>
</html>