JavaScript制作簡易計(jì)算器(不用eval)
更新時(shí)間:2017年02月05日 10:19:16 作者:萬花果子
這篇文章主要為大家詳細(xì)介紹了JavaScript制作簡易計(jì)算器的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了js制作簡易計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下
<head> <meta charset="UTF-8"> <title>Title</title> <style type="text/css"> * { padding: 0; margin: 0; } li { list-style: none; } body { background: #940032; } #counter { width: 500px; height: 420px; background: #939; margin: 50px auto 0; position: relative; } #counter h2 { line-height: 42px; padding-left: 15px; font-size: 14px; font-family: arial; color: #ff3333; } #counter a { font-weight: normal; text-decoration: none; color: #ff3333; } #counter a:hover { text-decoration: underline; } #bg { width: 280px; height: 200px; border: 3px solid #680023; background: #990033; filter: alpha(opacity=80); opacity: 0.8; position: absolute; left: 50%; top: 115px; margin-left: -141px; } #counter_content { width: 250px; position: absolute; top: 130px; left: 130px; z-index: 1; } #counter_content h3 { margin-bottom: 10px; } #counter_content h3 input { border: none; width: 223px; height: 30px; line-height: 30px; padding: 0 10px; background: url(img/ico.png) no-repeat; text-align: right; color: #333; font-size: 14px; font-weight: bold; } #counter_content div { width: 250px; } #counter_content input { width: 60px; height: 30px; line-height: 30px; float: left; background: url(img/ico.png) no-repeat -303px 0; text-align: center; color: #fff; cursor: pointer; margin: 0 1px 4px 0; border: 0; } #counter_content div > input:hover { background: url(img/ico.png) no-repeat -243px 0; } #counter p { width: 500px; position: absolute; bottom: 20px; left: 0; color: #ff3333; text-align: center; font-size: 12px; } </style> </head> <body> <div id="counter"> <h2>簡易計(jì)算</h2> <div id="counter_content"> <h3><input id="input1" type="text" value="0"/></h3> <div id="div1"> <input type="button" value="7" onclick="kick('7')"/> <input type="button" value="8" onclick="kick('8')"/> <input type="button" value="9" onclick="kick('9')"/> <input type="button" value="+" onclick="kick('+')"/> <input type="button" value="4" onclick="kick('4')"/> <input type="button" value="5" onclick="kick('5')"/> <input type="button" value="6" onclick="kick('6')"/> <input type="button" value="-" onclick="kick('-')"/> <input type="button" value="1" onclick="kick('1')"/> <input type="button" value="2" onclick="kick('2')"/> <input type="button" value="3" onclick="kick('3')"/> <input type="button" value="*" onclick="kick('*')"/> <input type="button" value="0" onclick="kick('0')"/> <input type="button" value="C" onclick="kick('C')"/> <input type="button" value="=" onclick="kick('=')"/> <input type="button" value="/" onclick="kick('/')"/> </div> </div> </div> </body> <script> var showInput = document.getElementById("input1"); var isClear = false; var tempStr = ""; var clacType = ""; var isContinue = true; function kick(clickValue) { switch (clickValue) { case "=": if (tempStr != "" && clacType != "") { showInput.value = clac(tempStr, showInput.value, clacType); isContinue = false; clacType = ""; } break; case "+": case "-": case "*": case "/": //如果預(yù)存的操作符不為空 表示表示連續(xù)操作 if (clacType != "" && !isContinue) { //先執(zhí)行計(jì)算 tempStr = clac(tempStr, showInput.value, clacType); isClear = true; clacType = clickValue; } else { tempStr = showInput.value; //點(diǎn)擊操作符之后 預(yù)存字符 isClear = true;//表示點(diǎn)擊了操作符 clacType = clickValue;//預(yù)存操作符 } isContinue = true; break; case "C": showInput.value = "0"; isClear = false; tempStr = ""; clacType = ""; break; default://普通的數(shù)字按鈕點(diǎn)擊 showInput.value = showInput.value == "0" ? "" : showInput.value; isContinue = false; if (isClear) { showInput.value = ""; showInput.value += clickValue; isClear = false; } else { showInput.value += clickValue; } break; } } function clac(num1, num2, type) { switch (type) { case "+": return Number(num1) + Number(num2); case "-": return Number(num1) - Number(num2); case "*": return Number(num1) * Number(num2); case "/": return Number(num1) / Number(num2); default: break; } } </script>
關(guān)于計(jì)算器的精彩文章請(qǐng)查看《計(jì)算器專題》 ,更多精彩等你來發(fā)現(xiàn)!
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
小程序?qū)崿F(xiàn)自定義導(dǎo)航欄適配完美版
這篇文章主要介紹了小程序?qū)崿F(xiàn)自定義導(dǎo)航欄適配完美版,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04淺析location.href跨窗口調(diào)用函數(shù)
本文詳細(xì)介紹了location.href跨窗口調(diào)用函數(shù),具體的使用方法及實(shí)例,有需要的朋友可以參考下2016-11-11JS動(dòng)畫定時(shí)器知識(shí)總結(jié)
這篇文章給大家總結(jié)了關(guān)于JS動(dòng)畫中定時(shí)器的相關(guān)用法以及相關(guān)知識(shí)點(diǎn)總結(jié),有需要的朋友可以參考學(xué)習(xí)下。2018-03-03基于微信小程序?qū)崿F(xiàn)人臉數(shù)量檢測的開發(fā)步驟
最近項(xiàng)目需求是統(tǒng)計(jì)當(dāng)前攝像頭中的人臉個(gè)數(shù),所以下面這篇文章主要給大家介紹了關(guān)于基于微信小程序?qū)崿F(xiàn)人臉數(shù)量檢測的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12