javascript實(shí)現(xiàn)簡易計(jì)算器的代碼
今天閑來無聊,想寫點(diǎn)什么,突然想到用javascript寫一個(gè)計(jì)算器。程序還存在很多的Bug,先在這里記錄一下,以后慢慢更正。
代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>javascript實(shí)現(xiàn)簡易計(jì)算器的代碼_腳本之家</title> <style type="text/css"> input{ width:30px; height:20px; text-align:center; } #tbCalculator td { text-align:center; vertical-align:middle; } </style> <script type="text/javascript"> var result; //保存點(diǎn)擊運(yùn)算符之前輸入框中的數(shù)值 var operator; //保存運(yùn)算符 var isPressEqualsKey = false; //記錄是否按下”=“鍵 //數(shù)字鍵事件 function connectionDigital(control) { var txt = document.getElementById('txtScream'); if(isPressEqualsKey) { txt.value = ""; //已進(jìn)行過計(jì)算,則清空數(shù)值輸入框重新開始 isPressEqualsKey = false; } //數(shù)值輸入已經(jīng)存在小數(shù)點(diǎn),則不允許再輸入小數(shù)點(diǎn) if(txt.value.indexOf('.') > -1 && control.value == '.') return false; txt.value += control.value; //將控件值賦給數(shù)值輸入框中 } //退格鍵事件 function backspace() { var txt = document.getElementById('txtScream'); txt.value = txt.value.substring(0,txt.value.length - 1); } //ce鍵事件:清空數(shù)字輸入框 function clearAll() { document.getElementById('txtScream').value = ""; result = ""; operator = ""; } // +、-、*、/ 事件 function calculation(control) { //將運(yùn)算符保存入全局變量中 operator = control.value; var txt = document.getElementById('txtScream'); if(txt.value == "")return false; //數(shù)值輸入框中沒有數(shù)字,則不能輸入運(yùn)算符 //將數(shù)值輸入框中的值保存到計(jì)算表達(dá)式中 result = txt.value; //清空輸入框,以待輸入操作值 txt.value = ""; } //計(jì)算結(jié)果 function getResult() { var opValue; //計(jì)算表達(dá)式中存在運(yùn)算符 var sourseValue = parseFloat(result); var txt = document.getElementById('txtScream'); if(operator == '*') opValue = sourseValue * parseFloat(txt.value); else if(operator == '/') opValue = sourseValue / parseFloat(txt.value); else if(operator == '+') opValue = sourseValue + parseFloat(txt.value); else if(operator == '-') opValue = sourseValue - parseFloat(txt.value); txt.value = opValue; isPressEqualsKey = true; result = ""; opValue = ""; } </script> </head> <body> <table id="tbCalculator" width="200" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#0066FF"> <tr> <td height="30" colspan="4" align="center"> <input type="text" name="txtScream" id="txtScream" style="width:180px; border-style:none; text-align:right;" readonly="readonly" /> </td> </tr> <tr> <td height="30" colspan="2"> <input type="button" name="btnCE" id="btnCE" value="C E" style="width:80px;" align="right"; onclick="clearAll();" /></td> <td height="30" colspan="2"> <input type="button" name="btn10" id="btn10" value="Backspace" style="width:80px;" align="right"; onclick="backspace();" /></td> </tr> <tr> <td height="30"><input type="button" name="btn7" id="btn7" value="7" onclick="connectionDigital(this);" /></td> <td><input type="button" name="btn8" id="btn8" value="8" onclick="connectionDigital(this);"/></td> <td><input type="button" name="btn9" id="btn9" value="9" onclick="connectionDigital(this);" /></td> <td><input type="button" name="btn6" id="btn6" value="/" onclick="calculation(this);" /></td> </tr> <tr> <td height="30"> <input type="button" name="btn4" id="btn4" value="4" onclick="connectionDigital(this);"/></td> <td><input type="button" name="btn5" id="btn5" value="5" onclick="connectionDigital(this);"/></td> <td><input type="button" name="btn6" id="btn6" value="6" onclick="connectionDigital(this);"/></td> <td><input type="button" name="btn13" id="btn13" value="*" onclick="calculation(this);" /></td> </tr> <tr> <td height="30"> <input type="button" name="btn1" id="btn1" value="1" onclick="connectionDigital(this);"/></td> <td><input type="button" name="btn2" id="btn2" value="2" onclick="connectionDigital(this);"/></td> <td><input type="button" name="btn3" id="btn3" value="3" onclick="connectionDigital(this);"/></td> <td><input type="button" name="btn18" id="btn18" value="-" onclick="calculation(this);" /></td> </tr> <tr> <td height="30"><input type="button" name="btn0" id="btn0" value="0" onclick="connectionDigital(this);"/></td> <td><input type="button" name="btndot" id="btndot" value="." onclick="connectionDigital(this);" /></td> <td><input name="btn22" type="button" id="btn22" value="=" onclick="getResult();" /></td> <td><input type="button" name="btn23" id="btn23" value="+" onclick="calculation(this);" /></td> </tr> </table> </body> </html>
以上這篇javascript實(shí)現(xiàn)簡易計(jì)算器的代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 簡易js代碼實(shí)現(xiàn)計(jì)算器操作
- js實(shí)現(xiàn)一個(gè)簡易計(jì)算器
- 簡易的JS計(jì)算器實(shí)現(xiàn)代碼
- JavaScript實(shí)現(xiàn)一個(gè)簡易的計(jì)算器實(shí)例代碼
- js制作簡易計(jì)算器
- JavaScript實(shí)現(xiàn)簡易加法計(jì)算器
- 超級簡易的JS計(jì)算器實(shí)例講解(實(shí)現(xiàn)加減乘除)
- js實(shí)現(xiàn)簡易計(jì)算器功能
- js實(shí)現(xiàn)一個(gè)簡易的計(jì)算器
- 用JavaScript做一個(gè)簡易計(jì)算器的三種方法舉例
相關(guān)文章
JS實(shí)現(xiàn)前端動(dòng)態(tài)分頁碼代碼實(shí)例
這篇文章主要介紹了JS實(shí)現(xiàn)前端動(dòng)態(tài)分頁碼代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06JavaScript中undefined、null與NaN的區(qū)別
undefined、null和NaN都屬于javascript中的數(shù)據(jù)類型,本文主要介紹了 JavaScript中undefined、null與NaN的區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09JavaScript實(shí)現(xiàn)的斑馬線表格效果【隔行變色】
這篇文章主要介紹了JavaScript實(shí)現(xiàn)的斑馬線表格效果,通過javascript針對table表格的遍歷與運(yùn)算實(shí)現(xiàn)隔行變色功能,非常簡單實(shí)用,需要的朋友可以參考下2017-09-09JS實(shí)現(xiàn)簡單的二元方程計(jì)算器功能示例
這篇文章主要介紹了JS實(shí)現(xiàn)簡單的二元方程計(jì)算器功能,涉及javascript數(shù)學(xué)運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2017-01-01使用JavaScript解決網(wǎng)頁圖片拉伸問題(推薦)
本文給大家介紹使用javascript解決網(wǎng)頁圖片拉伸問題,本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-11-11css值轉(zhuǎn)換成數(shù)值請拋棄parseInt
絕大多數(shù)人喜歡用parseInt()把css中的字符串值轉(zhuǎn)換成數(shù)值2011-10-10Bootstrap jquery.twbsPagination.js動(dòng)態(tài)頁碼分頁實(shí)例代碼
這篇文章主要介紹了Bootstrap jquery.twbsPagination.js動(dòng)態(tài)頁碼分頁實(shí)例代碼,需要的朋友可以參考下2017-02-02一個(gè)不用onmouseup的拖動(dòng)函數(shù)
一個(gè)不用onmouseup的拖動(dòng)函數(shù)...2007-05-05用javascript實(shí)現(xiàn)的支持lrc歌詞的播放器
用javascript實(shí)現(xiàn)的支持lrc歌詞的播放器...2007-05-05JavaScript 引用類型實(shí)例詳解【數(shù)組、對象、嚴(yán)格模式等】
這篇文章主要介紹了JavaScript 引用類型,結(jié)合實(shí)例形式詳細(xì)分析了JavaScript 數(shù)組、對象、嚴(yán)格模式引用類型相關(guān)概念、原理及操作注意事項(xiàng),需要的朋友可以參考下2020-05-05