计算器:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>计算器</title>
<script type="text/javascript">
window.onload = function() {
var nNum1 = document.getElementById('num1');
var nNum2 = document.getElementById('num2');
var mArk = document.getElementById('mark');
var oBtn = document.getElementById('butt');
oBtn.onclick = function () {
var val = nNum1.value;
var va2 = nNum2.value;
//不做此判断会弹出“NaN”
if (val == "" || va2 == "") {
alert('输入框不能为空!请重新输入');
return;
}
if (isNaN(val) || isNaN(va2)) {
alert('请输入数字!请重新输入');
return;
}
switch (mArk.value) {
case '0 ':
alert((parseFloat(val) * 100 + parseFloat(va2) * 100) / 100);
break;
case '1':
alert((parseFloat(val) * 100 - parseFloat(va2) * 100) / 100);
break;
case '2':
alert((parseFloat(val) * 100) * (parseFloat(va2) * 100) / 1000);
break;
case '3':
if (nNum1 != 0) {
alert((parseFloat(val) * 100) / (parseFloat(va2) * 100));
break;
} else if(nNum1 == 0) {
alert("被除数不能为0,你是笨蛋吗?这都不知道")
break;
}
}
}
}
</script>
</head>
<body>
<h1>计算器</h1>
<input type="text" name="" id="num1"/>
<!--下拉菜单-->
<select id="mark">
<option value="0">+</option>
<option value="1">-</option>
<option value="2">*</option>
<option value="3">/</option>
</select>
<input type="text" name="" id="num2"/>
<input type="button" value="计算" id="butt" name=""/>
</body>
</html>
还请各路大神多多指教
网友评论