這篇文章主要介紹了Javascript實(shí)現(xiàn)基本運(yùn)算器的方法,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)建站堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的城中網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
javascript是一種動(dòng)態(tài)類型、弱類型的語言,基于對(duì)象和事件驅(qū)動(dòng)并具有相對(duì)安全性并廣泛用于客戶端網(wǎng)頁開發(fā)的腳本語言,同時(shí)也是一種廣泛用于客戶端Web開發(fā)的腳本語言。它主要用來給HTML網(wǎng)頁添加動(dòng)態(tài)功能,現(xiàn)在JavaScript也可被用于網(wǎng)絡(luò)服務(wù)器,如Node.js。
用Javascript實(shí)現(xiàn)一個(gè)基本的運(yùn)算器,具體內(nèi)容如下
使用表格布局,JS添加事件
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>計(jì)算器</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=[];//定義一個(gè)數(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)時(shí),若計(jì)算器屏幕為"0", otext.value+=this.value; //實(shí)現(xiàn)輸入一個(gè)非零數(shù)字的時(shí)候,計(jì)算器 } //上的數(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)輸入一個(gè)運(yùn)算符的時(shí)候,otext內(nèi)的value值 arr[arr.length]=this.value; //為""(空),所以判斷條件為若otext內(nèi)的value值不為空 otext.value=""; //則向數(shù)字中傳值。 } //此時(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值為空, } //此時(shí)判斷若this的值為"-",就替換進(jìn)去。 else if(otext.value=="0"&&this.value=="-"){//此時(shí)出現(xiàn)無法執(zhí)行類似"3--3"的雙減法運(yùn)算BUG, otext.value=this.value; //因?yàn)閑val()無法識(shí)別有雙減的字符串值。 } //若初始時(shí),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ù)值的時(shí)候,判斷arr數(shù)組中的 otext.value=Math.abs(otext.value); //→末尾值是否為"-",若為"-"則把其改為"+", } //→并且讓otext.value值取絕對(duì)值。 arr[arr.length]=otext.value; //| for(var i=0;i<arr.length;i++){ //| string+=arr[i]; //| } //|把存入數(shù)組中的數(shù)字和運(yùn)算符遍歷存儲(chǔ)到一個(gè)字符串中, otext.value=eval(string); //|直接使用eval()方法就可以識(shí)別一個(gè)的字符串,執(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>
計(jì)算器的”c”功能為清屏;”d”功能為刪除一個(gè)數(shù);
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Javascript實(shí)現(xiàn)基本運(yùn)算器的方法”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
網(wǎng)站標(biāo)題:Javascript實(shí)現(xiàn)基本運(yùn)算器的方法
URL網(wǎng)址:http://aaarwkj.com/article38/jesjsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、面包屑導(dǎo)航、品牌網(wǎng)站設(shè)計(jì)、小程序開發(fā)、自適應(yīng)網(wǎng)站、營銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)