JavaScript實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)簡(jiǎn)單計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下
1.實(shí)現(xiàn)基本計(jì)算器功能,如圖
2.邏輯代碼
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>計(jì)算器</title> <!--設(shè)置樣式--> <style> .showdiv{ text-align: center; margin:auto;/*設(shè)置居中*/ border: solid 1px; width: 400px; height: 500px; border-radius: 10px;/*設(shè)置邊框角度*/ } input[type="text"]{ margin-top: 10px; width: 380px; height: 40px; font-size: 40px; } input[type="button"]{ margin: 10px; margin-top: 20px; width: 60px; height: 80px; font-size: 40px; font-weight: bold; } </style> <!--設(shè)置js代碼--> <script type="text/javascript"> /*將按鈕的值賦值給輸入框*/ function num(btn){ //把不能為零去掉 if(document.getElementById("inp").value.match("除數(shù)")){ document.getElementById("inp").value = ""; } //獲取button按鈕的value var num = btn.value; console.log(num +" " +typeof(num)) //將值賦值給text文本框 switch(num){ case "c": document.getElementById("inp").value = ""; break; case "=": if(document.getElementById("inp").value.match("/")){ if(document.getElementById("inp").value.split("/")[1] == "0"){ document.getElementById("inp").value = "除數(shù)不能為零"; }else{ document.getElementById("inp").value = eval(document.getElementById("inp").value); } break; }else{ document.getElementById("inp").value = eval(document.getElementById("inp").value); break; } default: document.getElementById("inp").value = document.getElementById("inp").value+num; break; } } </script> </head> <body> <div class = "showdiv"> <input type="text" name="" id="inp" value="" readonly="readonly"/><br /> <input type="button" name="" id="" value="0" onclick="num(this)"/> <input type="button" name="" id="" value="1" onclick="num(this)"/> <input type="button" name="" id="" value="2" onclick="num(this)"/> <input type="button" name="" id="" value="3" onclick="num(this)"/><br /> <input type="button" name="" id="" value="4" onclick="num(this)"/> <input type="button" name="" id="" value="5" onclick="num(this)"/> <input type="button" name="" id="" value="6" onclick="num(this)"/> <input type="button" name="" id="" value="7" onclick="num(this)"/><br /> <input type="button" name="" id="" value="8" onclick="num(this)"/> <input type="button" name="" id="" value="9" onclick="num(this)"/> <input type="button" name="" id="" value="+" onclick="num(this)"/> <input type="button" name="" id="" value="-" onclick="num(this)"/><br /> <input type="button" name="" id="" value="*" onclick="num(this)"/> <input type="button" name="" id="" value="/" onclick="num(this)"/> <input type="button" name="" id="" value="=" onclick="num(this)"/> <input type="button" name="" id="" value="c" onclick="num(this)"/> </div> </body> </html>
關(guān)于計(jì)算器的精彩文章請(qǐng)查看《計(jì)算器專題》 ,更多精彩等你來發(fā)現(xiàn)!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js將當(dāng)前時(shí)間格式轉(zhuǎn)換成時(shí)間搓(自寫)
將時(shí)間轉(zhuǎn)換成時(shí)間搓的方法有很多,在本文為大家介紹下使用js將當(dāng)前時(shí)間轉(zhuǎn)換成時(shí)間搓 例如2013-09-11 12:12:12,感興趣的朋友可以參考下2013-09-09微信小程序開發(fā)之實(shí)現(xiàn)搖色子游戲
這篇文章主要為大家詳細(xì)介紹了如何通過微信小程序開發(fā)一個(gè)簡(jiǎn)單的搖色子游戲,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以和小編一起學(xué)習(xí)一下2023-01-01javascript將數(shù)字轉(zhuǎn)換整數(shù)金額大寫的方法
這篇文章主要介紹了javascript將數(shù)字轉(zhuǎn)換整數(shù)金額大寫的方法,通過自定義函數(shù)中的數(shù)組替換實(shí)現(xiàn)數(shù)字轉(zhuǎn)換整數(shù)金額大寫的功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-01-01JS實(shí)現(xiàn)選項(xiàng)卡實(shí)例詳解
這篇文章主要介紹了JS實(shí)現(xiàn)選項(xiàng)卡的方法,結(jié)合實(shí)例形式詳細(xì)講述了JavaScript實(shí)現(xiàn)選項(xiàng)卡的布局與功能實(shí)現(xiàn)的完整步驟及相關(guān)技巧,需要的朋友可以參考下2015-11-11script標(biāo)簽中的async和defer詳細(xì)說明與使用場(chǎng)景
這篇文章主要介紹了script標(biāo)簽中的async和defer詳細(xì)說明與使用場(chǎng)景,需要的朋友可以參考下2023-02-02