<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
第一个数字<input type="text" id="num1" /> 选择计算类型
<select id="typeflag">
<option value="1">加法</option>
<option value="2">减法</option>
<option value="3">乘法</option>
<option value="4">除法</option>
</select>第二个数字
<input type="text" id="num2" />
<input type="button" value="计算" onclick="m1()" /> 结果:
<input type="text" id="num3" />
</body>
<script typr="text/javascript">
function m1() {
var x = document.getElementById("num1").value;
x = parseInt(x);
var y = document.getElementById("typeflag").value;
y = parseInt(y);
var z = document.getElementById("num2").value;
z = parseInt(z);
var a;
if (y == 1) {
a = x + z;
} else if (y == 2) {
a = x - z;
} else if (y == 3) {
a = x * z;
} else if (y == 4) {
if (z == 0) {
alert("除数不能为0")
} else {
a = x / z;
}
}
document.getElementById("num3").value = a;
}
</script>
</html>
简单计算器
最新推荐文章于 2024-07-12 16:27:40 发布