效果圖 index.html <!DOCTYPE html> <html> <head> <title></title> <link rel="stylesheet" type="text/css" href="calc.css"> <script type="text/javascript" sr ...
效果圖
index.html
<!DOCTYPE html> <html> <head> <title></title> <link rel="stylesheet" type="text/css" href="calc.css"> <script type="text/javascript" src="calc.js"></script> </head> <body onload="init()"> <div id="div1"> <div id="div2"> <input type="text" name="num" id="num" /> </div> <div id="div3"> <input type="button" value="c" name="" id=""/> <input type="button" value="←" name="" id=""/> <input type="button" value="+/-" name="" id=""/> <input type="button" value="/" name="" id=""/> <input type="button" value="1" name="" id="n1"/> <input type="button" value="2" name="" id=""/> <input type="button" value="3" name="" id=""/> <input type="button" value="*" name="" id=""/> <input type="button" value="4" name="" id=""/> <input type="button" value="5" name="" id=""/> <input type="button" value="6" name="" id=""/> <input type="button" value="-" name="" id=""/> <input type="button" value="7" name="" id=""/> <input type="button" value="8" name="" id=""/> <input type="button" value="9" name="" id=""/> <input type="button" value="+" name="" id=""/> <input type="button" value="0" name="" id=""/> <input type="button" value="." name="" id=""/> <input type="button" value="=" name="" id=""/> <input type="button" value="m" name="" id="imooc"/> </div> </div> </body> </html>
calc.css
*{ margin:0px; padding:0px; } div{ width: 170px; } #div1{ top:60px; left: 100px; position: absolute; } input[type="button"]{ width: 30px; margin-right: 5px; } input[type="text"]{ width: 152px; text-align: right; background-color: #fff; border: 1px solid; padding-right: 5px; box-sizing: border-box; } input[type="button"]:hover{ background-color: yellow; border: 1px solid; }
calc.js
function init(){ var num=document.getElementById("num"); num.value=0; num.disabled="disabled"; var oButton=document.getElementsByTagName("input"); var btn_num1; var fh; for(var i=0;i<oButton.length;i++){ oButton[i].onclick=function(){ if(isNumber(this.value)){ if(isNull(num.value)){ num.value=this.value; }else{ num.value=num.value+this.value; } }else{ var btn_num=this.value; switch(btn_num){ case "+": btn_num1=Number(num.value); num.value=0; fh="+"; break; case "-": btn_num1=Number(num.value); num.value=0; fh="-"; break; case "*": btn_num1=Number(num.value); num.value=0; fh="*"; break; case "/": btn_num1=Number(num.value); num.value=0; fh="/"; break; case ".": num.value=dec_number(num.value); break; case "←": num.value=back(num.value); break; case "c": num.value="0"; break; case "+/-": num.value=sign(num.value); break; case "=": switch(fh){ case "+": num.value=btn_num1+Number(num.value); break; case "-": num.value=btn_num1-Number(num.value); break; case "*": num.value=btn_num1*Number(num.value); break; case "/": if(Number(num.value)==0){ alert("除數不能是0"); num.value=0; }else{ num.value=btn_num1/Number(num.value); } break; } break; } } } } } /*正負號*/ function sign(n){ n=Number(n)*-1; return n; } /*退位鍵*/ function back(n){ n=n.substr(0,n.length-1); if(isNull(n)){ n="0"; } return n; } /*小數點*/ function dec_number(n){ if(n.indexOf(".")==-1){ n=n+"."; } return n; } /*驗證文本框是否為空或者0*/ function isNull(n){ if(n=="0" || n.length==0){ return true; }else{ return false; } } function isNumber(n){ return !isNaN(n); }