一個簡單的jQuery計算機實現了連續計算功能_jquery

來源:互聯網
上載者:User

一個簡單的jQuery計算機,只是實現了一個連續計算的功能

<!DOCTYPE html> <html> <head> <meta charset="utf8"/> <title>Javascript計算機</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(); //符號集合 symarr[0] = '+'; symarr[1] = '/'; symarr[2] = '*'; symarr[3] = '-'; symarr[4] = '='; function number(n){ var numnow = $('#show'); var _exist=$.inArray(lastclick,symarr); //判斷上次點擊是否為符號 if (numnow.val() == 0 || _exist != -1) {//若顯示框為0 或者上次點擊為符號,則重新記錄顯示框 numnow.val($(n).val()); }else{ numnow.val(parseInt(numnow.val()) * 10 +parseInt($(n).val())); } lastclick = $(n).val(); // 更新上次點擊 } function calsym(cs){ //符號點擊事件響應 var numnow = $('#show'); var _exist=$.inArray(lastclick,symarr); if (num0 == null && symble == null) { //初始狀態 num0 = numnow.val(); symble = $(cs).val(); }else if(num0 != null && num1 == null && _exist !=-1){ // 連續點操作符的處理,及第一次點擊操作符處理 symble = $(cs).val(); }else{ //正常計算狀態 num1 = numnow.val(); var result = calculate(symble,num0,num1); symble = $(cs).val(); //上行及本行:先計算之前的運算子及結果,再更新運算子為本次點擊 num0 = result; numnow.val(result); num1 = null; } lastclick = $(cs).val(); // 更新上次點擊 } function clearCal(){ //清空函數 $('#show').val('0'); num0 = null; num1 = null; symble = null; lastclick = null; } function calculate(sym,m,n){ //計算並返回結果 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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.