<!DOCTYPE html> <html> <head> <meta charset="utf8"/> <title>Javascript計(jì)算器</title> </head> <body> <table> <tr> <td colspan="4"><input id="show" value="0"/></td> </tr> <tr> <td><button onclick="number(this)" value="7">7</button></td> <td><button onclick="number(this)" value="8">8</button></td> <td><button onclick="number(this)" value="9">9</button></td> <td><button onclick="calsym(this)" value="+">+</button></td> </tr> <tr> <td><button onclick="number(this)" value="4">4</button></td> <td><button onclick="number(this)" value="5">5</button></td> <td><button onclick="number(this)" value="6">6</button></td> <td><button onclick="calsym(this)" value="-">-</button></td> </tr> <tr> <td><button onclick="number(this)" value="1">1</button></td> <td><button onclick="number(this)" value="2">2</button></td> <td><button onclick="number(this)" value="3">3</button></td> <td><button onclick="calsym(this)" value="*">*</button></td> </tr> <tr> <td><button onclick="number(this)" value="0">0</button></td> <td><button onclick="calsym(this)" value="=">=</button></td> <td><button onclick="clearCal()" value="c">c</button></td> <td><button onclick="calsym(this)" value="/">/</button></td> </tr> </table> </body> <script type="text/javascript" src="jquery-1.9.1.js"></script> <script type="text/javascript" src="jquery-1.9.1.min.js"></script> <script type="text/javascript"> var num0 = null; var num1 = null; var symble = null; var lastclick = null; var symarr = new Array(); //符號(hào)集合 symarr[0] = '+'; symarr[1] = '/'; symarr[2] = '*'; symarr[3] = '-'; symarr[4] = '='; function number(n){ var numnow = $('#show'); var _exist=$.inArray(lastclick,symarr); //判斷上次點(diǎn)擊是否為符號(hào) if (numnow.val() == 0 || _exist != -1) {//若顯示框?yàn)? 或者上次點(diǎn)擊為符號(hào),則重新記錄顯示框 numnow.val($(n).val()); }else{ numnow.val(parseInt(numnow.val()) * 10 +parseInt($(n).val())); } lastclick = $(n).val(); // 更新上次點(diǎn)擊 } function calsym(cs){ //符號(hào)點(diǎn)擊事件響應(yīng) var numnow = $('#show'); var _exist=$.inArray(lastclick,symarr); if (num0 == null && symble == null) { //初始狀態(tài) num0 = numnow.val(); symble = $(cs).val(); }else if(num0 != null && num1 == null && _exist !=-1){ // 連續(xù)點(diǎn)操作符的處理,及第一次點(diǎn)擊操作符處理 symble = $(cs).val(); }else{ //正常計(jì)算狀態(tài) num1 = numnow.val(); var result = calculate(symble,num0,num1); symble = $(cs).val(); //上行及本行:先計(jì)算之前的運(yùn)算符及結(jié)果,再更新運(yùn)算符為本次點(diǎn)擊 num0 = result; numnow.val(result); num1 = null; } lastclick = $(cs).val(); // 更新上次點(diǎn)擊 } function clearCal(){ //清空函數(shù) $('#show').val('0'); num0 = null; num1 = null; symble = null; lastclick = null; } function calculate(sym,m,n){ //計(jì)算并返回結(jié)果 var res = null; m = parseInt(m); n = parseInt(n); switch(sym){ case '+': res = m+n; break; case '-': res = m-n; break; case '*': res = m*n; break; case '/': if (n == 0) { alert("false"); break; } res = m/n; break; default: break; } return res; } </script> </html>
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com