基于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>
希望本文所述對(duì)大家學(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ù)中加號(hào)和&號(hào)自動(dòng)改變的方法
下面小編就為大家?guī)硪黄昝澜鉀Qjs傳遞參數(shù)中加號(hào)和&號(hào)自動(dòng)改變的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10
動(dòng)態(tài)加載腳本提升javascript性能
動(dòng)態(tài)加載腳本可以有效提升javascript性能,下面有個(gè)不錯(cuò)的示例,大家可以參考下2014-02-02
JavaScript中的閉包(Closure)詳細(xì)介紹
這篇文章主要介紹了JavaScript中的閉包(Closure)詳細(xì)介紹,函數(shù)調(diào)用對(duì)象與變量的作用域鏈、什么是閉包等內(nèi)容,并給出了實(shí)例,需要的朋友可以參考下2014-12-12
Js中安全獲取Object深層對(duì)象的方法實(shí)例
Object是JavaScript基本數(shù)據(jù)類型之一(function也屬于object,是特殊的object),其存儲(chǔ)于堆中,這篇文章主要給大家介紹了關(guān)于Js中安全獲取Object深層對(duì)象的相關(guān)資料,需要的朋友可以參考下2021-09-09
整理Javascript數(shù)組學(xué)習(xí)筆記
整理Javascript數(shù)組學(xué)習(xí)筆記,之前一系列的文章是跟我學(xué)習(xí)Javascript,本文就是進(jìn)一步學(xué)習(xí)javascript數(shù)組,希望大家繼續(xù)關(guān)注2015-11-11
js實(shí)現(xiàn)完美兼容各大瀏覽器的人民幣大小寫相互轉(zhuǎn)換
在基于網(wǎng)頁的打印輸出或報(bào)表中,經(jīng)常會(huì)牽扯到金額的大寫,每次都打上去很麻煩,所以想法用一個(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è)聚合對(duì)象中各個(gè)元素,而又不暴露該對(duì)象內(nèi)部表示,需要的朋友可以參考下2015-03-03
JavaScript中使用replace結(jié)合正則實(shí)現(xiàn)replaceAll的效果
JavaScript?中使用?replace?達(dá)到?replaceAll的效果,其實(shí)就用利用的正則的全局替換。2010-06-06

