看不懂的请留言, 有bug 请提出
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>加减乘除计算器</title>
<script type="text/javascript">
window.onload=function(){
var text1=document.getElementById("ct1");
var text2=document.getElementById("ct2");
var text3=document.getElementById("ct3");
var button=document.getElementById("btn");
var drop=document.getElementById("drop");
button.onclick=function(){
var a=parseInt(text1.value);
var b=parseInt(text2.value);
if (myIsNaN(a) && myIsNaN(b)){
}
else{
alert("输入必须是数字,不要拿其他东西忽悠我")
}
if (drop.value=="+"){
var c=(a+b);
if (myIsNaN(c)){text3.value=c;}
}
else if(drop.value=="-"){
var c=(a-b);
if (myIsNaN(c)){text3.value=c;}
}
else if(drop.value=="*"){
var c=(a*b);
if (myIsNaN(c)){text3.value=c;}
}
else if(drop.value=="/"){
if (b==0){
alert("不要输入0, 大哥我也不容易,就不要为难小弟了")
return;
}
var c=(a/b);
if (myIsNaN(c)){text3.value=c;}
}
}
// true:数值型的,false:非数值型
function myIsNaN(value) {
return typeof value === 'number' && !isNaN(value);
}
}
</script>
</head>
<body>
<input type="text" id="ct1">
<select name="" id="drop">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" id="ct2">
<input type="button" id="btn" value="计算">
<input type="text" id="ct3">
</body>
</html>