JavaScript實現(xiàn)簡單計算器功能
更新時間:2019年12月19日 15:25:39 作者:鄭德帥
這篇文章主要為大家詳細介紹了JavaScript實現(xiàn)簡單計算器功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了JavaScript實現(xiàn)簡單計算器的具體代碼,供大家參考,具體內容如下
1.實現(xiàn)基本計算器功能,如圖
2.邏輯代碼
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>計算器</title> <!--設置樣式--> <style> .showdiv{ text-align: center; margin:auto;/*設置居中*/ border: solid 1px; width: 400px; height: 500px; border-radius: 10px;/*設置邊框角度*/ } 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> <!--設置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>
關于計算器的精彩文章請查看《計算器專題》 ,更多精彩等你來發(fā)現(xiàn)!
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
javascript將數(shù)字轉換整數(shù)金額大寫的方法
這篇文章主要介紹了javascript將數(shù)字轉換整數(shù)金額大寫的方法,通過自定義函數(shù)中的數(shù)組替換實現(xiàn)數(shù)字轉換整數(shù)金額大寫的功能,非常具有實用價值,需要的朋友可以參考下2015-01-01script標簽中的async和defer詳細說明與使用場景
這篇文章主要介紹了script標簽中的async和defer詳細說明與使用場景,需要的朋友可以參考下2023-02-02