基于javascript實(shí)現(xiàn)簡單計(jì)算器功能
本文實(shí)例為大家介紹javascript實(shí)現(xiàn)簡單計(jì)算器功能的詳細(xì)代碼,分享給大家供大家參考,具體內(nèi)容如下
效果圖:
實(shí)現(xiàn)代碼:
<html> <head> <script> function calc(event){ // test //window.alert(event.value); var val = new String(event.value); // clear space val = val.trim(); var res = document.getElementById("res"); // clear if(val == "clear"){ res.value = ""; } // back if(val == "back"){ res.value = res.value.substring(0, res.value.length - 1); } // power if(val == "power"){ val = "p"; } // add val to text if(val.length == 1 && val != "="){ res.value = res.value + val; } // calc result if(val == "="){ var arr; var result; // power if(res.value.indexOf("p") != -1){ arr = res.value.split("p"); //window.alert(arr); result = Math.pow(parseFloat(arr[0]) ,parseFloat(arr[1])); //window.alert(res); res.value = result; } // plus if(res.value.indexOf("+") != -1){ arr = res.value.split("+"); //window.alert(arr); result = parseFloat(arr[0]) + parseFloat(arr[1]); //window.alert(res); res.value = result; } else if(res.value.indexOf("-") != -1){ // minus arr = res.value.split("-"); //window.alert(arr); result = parseFloat(arr[0]) - parseFloat(arr[1]); //window.alert(res); res.value = result; } else if(res.value.indexOf("*") != -1){ // multiply arr = res.value.split("*"); //window.alert(arr); result = parseFloat(arr[0]) * parseFloat(arr[1]); //window.alert(res); res.value = result; } else if(res.value.indexOf("/") != -1){ // division arr = res.value.split("/"); //window.alert(arr); result = parseFloat(arr[0]) / parseFloat(arr[1]); //window.alert(res); res.value = result; } else if(res.value.indexOf("%") != -1){ // module arr = res.value.split("%"); //window.alert(arr); result = parseFloat(arr[0]) % parseFloat(arr[1]); //window.alert(res); res.value = result; } } } </script> </head> <body> <table border="1px" cellpadding="10px" cellspacing="5px" align="center"> <tr align="center"> <td colspan="4"><input type="text" id="res" size="35px" value="" style="text-align:right;"/></td> </tr> <tr align="center"> <td><input type="button" value="power" onclick="calc(this)"/></td> <td><input type="button" value="clear" onclick="calc(this)"/></td> <td colspan="2"><input type="button" value=" back " onclick="calc(this)"/></td> </tr> <tr align="center"> <td><input type="button" value=" 1 " onclick="calc(this)"/></td> <td><input type="button" value=" 2 " onclick="calc(this)"/></td> <td><input type="button" value=" 3 " onclick="calc(this)"/></td> <td><input type="button" value=" + " onclick="calc(this)"/></td> </tr> <tr align="center"> <td><input type="button" value=" 4 " onclick="calc(this)"/></td> <td><input type="button" value=" 5 " onclick="calc(this)"/></td> <td><input type="button" value=" 6 " onclick="calc(this)"/></td> <td><input type="button" value=" - " onclick="calc(this)"/></td> </tr> <tr align="center"> <td><input type="button" value=" 7 " onclick="calc(this)"/></td> <td><input type="button" value=" 8 " onclick="calc(this)"/></td> <td><input type="button" value=" 9 " onclick="calc(this)"/></td> <td><input type="button" value=" * " onclick="calc(this)"/></td> </tr> <tr align="center"> <td><input type="button" value=" 0 " onclick="calc(this)"/></td> <td><input type="button" value=" = " onclick="calc(this)"/></td> <td><input type="button" value=" % " onclick="calc(this)"/></td> <td><input type="button" value=" / " onclick="calc(this)"/></td> </tr> </table> </body> </html>
希望本文所述對大家學(xué)習(xí)javascript程序設(shè)計(jì)有所幫助。
相關(guān)文章
JS實(shí)現(xiàn)具備延時(shí)功能的滑動(dòng)門菜單效果
這篇文章主要介紹了JS實(shí)現(xiàn)具備延時(shí)功能的滑動(dòng)門菜單效果,涉及JavaScript基于鼠標(biāo)事件與時(shí)間函數(shù)實(shí)現(xiàn)頁面樣式延遲變換功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09完美解決js傳遞參數(shù)中加號和&號自動(dòng)改變的方法
下面小編就為大家?guī)硪黄昝澜鉀Qjs傳遞參數(shù)中加號和&號自動(dòng)改變的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10動(dòng)態(tài)加載腳本提升javascript性能
動(dòng)態(tài)加載腳本可以有效提升javascript性能,下面有個(gè)不錯(cuò)的示例,大家可以參考下2014-02-02JavaScript中的閉包(Closure)詳細(xì)介紹
這篇文章主要介紹了JavaScript中的閉包(Closure)詳細(xì)介紹,函數(shù)調(diào)用對象與變量的作用域鏈、什么是閉包等內(nèi)容,并給出了實(shí)例,需要的朋友可以參考下2014-12-12整理Javascript數(shù)組學(xué)習(xí)筆記
整理Javascript數(shù)組學(xué)習(xí)筆記,之前一系列的文章是跟我學(xué)習(xí)Javascript,本文就是進(jìn)一步學(xué)習(xí)javascript數(shù)組,希望大家繼續(xù)關(guān)注2015-11-11js實(shí)現(xiàn)完美兼容各大瀏覽器的人民幣大小寫相互轉(zhuǎn)換
在基于網(wǎng)頁的打印輸出或報(bào)表中,經(jīng)常會牽扯到金額的大寫,每次都打上去很麻煩,所以想法用一個(gè)JavaScript客戶端腳本來實(shí)現(xiàn)自動(dòng)轉(zhuǎn)換,只需在需要顯示大寫金額的時(shí)候調(diào)用該JS函數(shù),下面我們就來匯總下吧2015-10-10深入理解JavaScript系列(35):設(shè)計(jì)模式之迭代器模式詳解
這篇文章主要介紹了深入理解JavaScript系列(35):設(shè)計(jì)模式之迭代器模式詳解,迭代器模式(Iterator):提供一種方法順序一個(gè)聚合對象中各個(gè)元素,而又不暴露該對象內(nèi)部表示,需要的朋友可以參考下2015-03-03JavaScript中使用replace結(jié)合正則實(shí)現(xiàn)replaceAll的效果
JavaScript?中使用?replace?達(dá)到?replaceAll的效果,其實(shí)就用利用的正則的全局替換。2010-06-06