简易计算器
程序员文章站
2022-03-27 09:59:29
...
作业如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>简易计算器</title>
<style>
input {height: 30px; line-height: 30px; padding: 0 10px; width: 60px; border: solid 1px #ddd}
select {height: 30px; line-height: 30px; padding: 0 10px;border: solid 1px #ddd}
button {background-color: dodgerblue; color: #fff; border: none; height: 30px; line-height: 30px; padding: 0 10px; cursor: pointer}
#jieguo {margin-top: 10px; color: #f00}
</style>
</head>
<body>
<input type="text" name="a" id="a" value="">
<select name="y" id="y">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" name="b" id="b" value="2">
<button id="btn">计算</button>
<div id="jieguo">?</div>
<script>
document.getElementById('btn').onclick=function () {
var a = document.getElementById('a').value;
var b = document.getElementById('b').value;
var y = document.getElementById('y').value;
a = parseInt(a);
b = parseInt(b);
var tiaojian = true; // 输入条件符合
var n;
// 输入情况判断
if (!a){
n = '第一个值不能为空或者不是数字';
tiaojian = false
}
if (!b){
var n = 1111;
alert('第二个值不能为空或者不是数字')
tiaojian = false
}
// if判断法
if (tiaojian==true){
if (y=='+'){
n = a+"+"+b+"="+(a+b);
}else if (y=='-'){
n = a+"-"+b+"="+(a-b);
}else if (y=='*'){
n = a+"*"+b+"="+(a*b);
}else if (y=='/'){
n = a+"/"+b+"="+(a/b);
}
}
document.getElementById('jieguo').innerHTML=n;
}
</script>
</body>
</html>
上一篇: Java应用程序性能调优技术