Javascript實(shí)現(xiàn)基本運(yùn)算器
用Javascript實(shí)現(xiàn)一個基本的運(yùn)算器,具體內(nèi)容如下
使用表格布局,JS添加事件
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>計算器</title>
</head>
<style>
*{
margin: 0;
padding: 0;
}
table{
width:400px;
height: 600px;
border-collapse: collapse;
margin: auto;
}
.trr{
width: 400px;
height: 100px;
}
.trr1{
width: 400px;
height: 50px;
}
.tdd{ width: 100px;
height: 100px;
border: 1px solid silver;
text-align: center;
line-height: 100px;
}
.btn{
width: 100%;
height: 100%;
font-size: 2.5em;
}
.btn1{
width: 100%;
height: 100%;
font-size: 2.5em;
}
.tdd1{
width: 100px;
height: 50px;
}
.text{
height: 100%;
font-size: 2.5em;
text-align: right;
}
</style>
<body>
<table>
<tr class="trr text1">
<td class="tdd" colspan="4"><input class="text" type="text" disabled value="0" /></td>
</tr>
<tr class="trr1">
<td class="tdd1" colspan="2"><input class="btn1 btn" type="button" value="c"/></td>
<td class="tdd1" colspan="2"><input class="btn1 btn" type="button" value="d"/></td>
</tr>
<tr class="trr">
<td class="tdd"><input class="btn" type="button" value="7"/></td>
<td class="tdd"><input class="btn" type="button" value="8"/></td>
<td class="tdd"><input class="btn" type="button" value="9"/></td>
<td class="tdd"><input class="btn" type="button" value="/"/></td>
</tr>
<tr class="trr">
<td class="tdd"><input class="btn" type="button" value="4"/></td>
<td class="tdd"><input class="btn" type="button" value="5"/></td>
<td class="tdd"><input class="btn" type="button" value="6"/></td>
<td class="tdd"><input class="btn" type="button" value="*"/></td>
</tr>
<tr class="trr">
<td class="tdd"><input class="btn" type="button" value="1"/></td>
<td class="tdd"><input class="btn" type="button" value="2"/></td>
<td class="tdd"><input class="btn" type="button" value="3"/></td>
<td class="tdd"><input class="btn" type="button" value="-"/></td>
</tr>
<tr class="trr">
<td class="tdd"><input class="btn" type="button" value="0"/></td>
<td class="tdd"><input class="btn" type="button" value="."/></td>
<td class="tdd"><input class="btn" type="button" value="+"/></td>
<td class="tdd"><input class="btn" type="button" value="="/></td>
</tr>
</table>
<script>
var obtn=document.getElementsByClassName("btn");
var otext=document.getElementsByClassName("text")[0];
var arr=[];//定義一個數(shù)組,向其中存入數(shù)字和運(yùn)算符。
for(var i=0;i<obtn.length;i++){
obtn[i].onclick= function () {
if(!isNaN(this.value)||this.value=="."){ //this:代表鼠標(biāo)點(diǎn)擊的obtn
if(otext.value.indexOf(".")==-1){ //消除重復(fù)"."的BUG
if(otext.value.length==0){
if(this.value!="0"){ //----------------------
otext.value+=this.value; //|
} //|
} //|
else if(otext.value.length==1&&otext.value=="0"){//|
otext.value=this.value; //|
} //|
else if(otext.value.length==1&&otext.value!="0"){//初始狀態(tài)時,若計算器屏幕為"0",
otext.value+=this.value; //實(shí)現(xiàn)輸入一個非零數(shù)字的時候,計算器
} //上的數(shù)值替換為輸入的非零值
else if(otext.value.length>1){ //|
otext.value+=this.value; //|
} //--------------------
}
else
{
if(this.value!="."){ //消除重復(fù)"."的BUG
if(otext.value.length==0){
if(obtn[i].value!="0"){
otext.value+=this.value;
}
}
if(otext.value.length>=1){
otext.value+=this.value;
}
}
}
}
if(this.value=="/"||this.value=="*"||this.value=="+"||this.value=="-"){
if(otext.value!="0"&&otext.value!=""&&otext.value!="-"){
//消除輸入重復(fù)運(yùn)算符的BUG,
arr[arr.length]=otext.value; //當(dāng)輸入一個運(yùn)算符的時候,otext內(nèi)的value值
arr[arr.length]=this.value; //為""(空),所以判斷條件為若otext內(nèi)的value值不為空
otext.value=""; //則向數(shù)字中傳值。
} //此時出現(xiàn)無法輸入負(fù)數(shù)值運(yùn)算的BUG
else if(otext.value==""&&this.value=="-"){ //消除無法輸入負(fù)數(shù)值運(yùn)算的BUG
otext.value=this.value; //當(dāng)點(diǎn)擊運(yùn)算符后otext的value值為空,
} //此時判斷若this的值為"-",就替換進(jìn)去。
else if(otext.value=="0"&&this.value=="-"){//此時出現(xiàn)無法執(zhí)行類似"3--3"的雙減法運(yùn)算BUG,
otext.value=this.value; //因?yàn)閑val()無法識別有雙減的字符串值。
} //若初始時,otext值為"0",并且this的值為
} //"-",則用"-"替換otext中的值。
if(this.value=="="){
if(otext.value.length>=1){ //--------------------
var string=""; //|
if(arr[arr.length-1]=="-"&&otext.value<0){ //→消除無法執(zhí)行類似"3--3"的雙減法運(yùn)算的BUG
arr[arr.length-1]="+"; //→當(dāng)輸入負(fù)數(shù)值的時候,判斷arr數(shù)組中的
otext.value=Math.abs(otext.value); //→末尾值是否為"-",若為"-"則把其改為"+",
} //→并且讓otext.value值取絕對值。
arr[arr.length]=otext.value; //|
for(var i=0;i<arr.length;i++){ //|
string+=arr[i]; //|
} //|把存入數(shù)組中的數(shù)字和運(yùn)算符遍歷存儲到一個字符串中,
otext.value=eval(string); //|直接使用eval()方法就可以識別一個的字符串,執(zhí)行
arr=[]; //|該字符串中的運(yùn)算
} //|
} //---------------------
if(this.value=="c"){
otext.value="0";
arr=[];
}
if(this.value=="d"){
otext.value=otext.value.substr(0,otext.value.length-1);//每一次刪除otext中的末尾值
if(otext.value==""){ //當(dāng)把otext中的值刪除完后,給
otext.value="0"; //otext復(fù)值"0".
}
}
}
}
</script>
</body>
</html>

計算器的”c”功能為清屏;”d”功能為刪除一個數(shù);
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
bootstrap table實(shí)現(xiàn)橫向合并與縱向合并
這篇文章主要為大家詳細(xì)介紹了bootstrap table實(shí)現(xiàn)橫向合并與縱向合并,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07
js獲取元素到可視區(qū)的距離、瀏覽器窗口滾動距離及元素距離瀏覽器頂部距離
這篇文章主要給大家介紹了關(guān)于js獲取元素到可視區(qū)的距離、瀏覽器窗口滾動距離及元素距離瀏覽器頂部距離的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-11-11
JavaScript實(shí)現(xiàn)的背景自動變色代碼
這篇文章主要介紹了JavaScript實(shí)現(xiàn)的背景自動變色代碼,涉及JavaScript數(shù)組操作結(jié)合定時函數(shù)實(shí)現(xiàn)修改頁面元素樣式的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
GoJs中導(dǎo)出圖片或者SVG實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了GoJs中導(dǎo)出圖片或者SVG實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
教你用javascript實(shí)現(xiàn)隨機(jī)標(biāo)簽云效果_附代碼
下面小編就為大家?guī)硪黄棠阌胘avascript實(shí)現(xiàn)隨機(jī)標(biāo)簽云效果_附代碼。小編覺得很實(shí)用,現(xiàn)在分享給大家。給大家一個參考2016-03-03
javascript定時器的簡單應(yīng)用示例【控制方塊移動】
這篇文章主要介紹了javascript定時器的簡單應(yīng)用,結(jié)合javascript事件觸發(fā)控制方塊移動操作分析了javascript定時器使用相關(guān)操作技巧,需要的朋友可以參考下2019-06-06

