实现简易的计算器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<input type="text" id="num1">
<select id="info">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
<option value="%">%</option>
</select>
<input type="text" id="num2">
<input type="button" value="=" id="btn">
<input type="text" id="res">
<script>
btn.onclick = function () {
var val1 = num1.value;
var val2 = num2.value;
var val3 = info.value;
switch (val3) {
case "+": res.value = parseInt(val1) + parseInt(val2); break;
case "-": res.value = val1 - val2; break;
case "*": res.value = val1 * val2; break;
case "/": res.value = val1 / val2; break;
case "%": res.value = val1 % val2; break;
}
}
</script>
</body>
</html>
![](https://img2018.cnblogs.com/blog/1523891/201908/1523891-20190829213653055-236635023.png)