Javascript實現(xiàn)基本運算器
更新時間:2017年07月15日 10:14:44 作者:梵海pp
這篇文章主要為大家詳細(xì)介紹了Javascript實現(xiàn)基本運算器的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
用Javascript實現(xià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ù)字和運算符。 for(var i=0;i<obtn.length;i++){ obtn[i].onclick= function () { if(!isNaN(this.value)||this.value=="."){ //this:代表鼠標(biāo)點擊的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; //實現(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ù)運算符的BUG, arr[arr.length]=otext.value; //當(dāng)輸入一個運算符的時候,otext內(nèi)的value值 arr[arr.length]=this.value; //為""(空),所以判斷條件為若otext內(nèi)的value值不為空 otext.value=""; //則向數(shù)字中傳值。 } //此時出現(xiàn)無法輸入負(fù)數(shù)值運算的BUG else if(otext.value==""&&this.value=="-"){ //消除無法輸入負(fù)數(shù)值運算的BUG otext.value=this.value; //當(dāng)點擊運算符后otext的value值為空, } //此時判斷若this的值為"-",就替換進(jìn)去。 else if(otext.value=="0"&&this.value=="-"){//此時出現(xiàn)無法執(zhí)行類似"3--3"的雙減法運算BUG, otext.value=this.value; //因為eval()無法識別有雙減的字符串值。 } //若初始時,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"的雙減法運算的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ù)字和運算符遍歷存儲到一個字符串中, otext.value=eval(string); //|直接使用eval()方法就可以識別一個的字符串,執(zhí)行 arr=[]; //|該字符串中的運算 } //| } //--------------------- 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)文章
?javascript數(shù)組中的lastIndexOf方法
這篇文章主要介紹了?javascript數(shù)組中的lastIndexOf方法,該方法可返回一個指定的字符串值最后出現(xiàn)的位置,在一個字符串中的指定位置從后向前搜索,下文詳細(xì)內(nèi)容需要的小伙伴可以參考一下2022-03-03你可能不知道的JavaScript的new Function()方法
JavaScript的精神領(lǐng)袖Douglas Crockford曾說過JavaScript是程序員唯一不需要學(xué)習(xí)就能直接使用的語言. 在編程中確實是如此2014-04-04JavaScript控制各種瀏覽器全屏模式的方法、屬性和事件介紹
瀏覽器全屏模式的啟動函數(shù)requestFullscreen仍然需要附帶各瀏覽器的js方言前綴,相信下面這段代碼需要你花大量的搜索才能湊齊:2014-04-04

javascript設(shè)計模式 – 建造者模式原理與應(yīng)用實例分析
這篇文章主要介紹了javascript設(shè)計模式 – 建造者模式,結(jié)合實例形式分析了javascript建造者模式相關(guān)概念、原理、應(yīng)用場景及操作注意事項,需要的朋友可以參考下
2020-04-04