JavaScript實現(xiàn)簡單計算器功能
更新時間:2019年12月19日 15:25:39 作者:鄭德帥
這篇文章主要為大家詳細介紹了JavaScript實現(xiàn)簡單計算器功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了JavaScript實現(xiàn)簡單計算器的具體代碼,供大家參考,具體內(nèi)容如下
1.實現(xiàn)基本計算器功能,如圖

2.邏輯代碼
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>計算器</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)于計算器的精彩文章請查看《計算器專題》 ,更多精彩等你來發(fā)現(xiàn)!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Ext JS動態(tài)加載JavaScript創(chuàng)建窗體的方法
這篇文章主要介紹了Ext JS動態(tài)加載JavaScript創(chuàng)建窗體的方法 ,需要的朋友可以參考下2016-06-06
基于Web Audio API實現(xiàn)音頻可視化效果
這篇文章主要介紹了基于Web Audio API實現(xiàn)音頻可視化效果,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06

