js實(shí)現(xiàn)簡單計(jì)算器
參考部分資料,編寫一個(gè)簡單的計(jì)算器案例,雖然完成了正常需求,但是也有不滿之處,待后續(xù)實(shí)力提升后再來補(bǔ)充,先把不足之處列出:
1:本來打算只要打開頁面,計(jì)算器的輸入框會(huì)顯示一個(gè)默認(rèn)為0的狀態(tài),但是在輸入框加入默認(rèn)顯示為0的時(shí)候,選擇數(shù)據(jù)輸入時(shí),該0會(huì)顯示輸入數(shù)字的前面,例如”0123“,由于能力有限,待后續(xù)實(shí)力提升再來補(bǔ)充完善!
2:目前只能實(shí)現(xiàn)鼠標(biāo)控制選擇按鈕,待完善鍵盤錄入功能。
3:乘法的那個(gè)符號(hào)在本來想改成”ד這個(gè)符號(hào)的,待后續(xù)完善。
附圖片一張:

html部分:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>計(jì)算器</title>
</head>
<body onload="onLoad()">
<div id="calculator">
<div class="LOGO">
<span class="name">簡單的計(jì)算器</span>
<span class="verson">@liumobai v1.0</span>
</div>
<div id="shuRu">
<!--screen輸入欄-->
<div class="screen">
<input type="text" id="screenName" name="screenName" class="screen" value="" onfocus="jsq(this)">
</div>
</div>
<div id="keys">
<!-- operators and other keys -->
<!--第一排-->
<input type="button" id="7" onclick="jsq(this.id)" value="7" class="buttons">
<input type="button" id="8" onclick="jsq(this.id)" value="8" class="buttons">
<input type="button" id="9" onclick="jsq(this.id)" value="9" class="buttons">
<input type="button" id="Back" onclick="tuiGe()" value="Back" class="buttons">
<input type="button" id="C" onclick="clearNum()" value="C" class="buttons" style="margin-right:0px">
<!--第二排-->
<input type="button" id="4" onclick="jsq(this.id)" value="4" class="buttons">
<input type="button" id="5" onclick="jsq(this.id)" value="5" class="buttons">
<input type="button" id="6" onclick="jsq(this.id)" value="6" class="buttons">
<input type="button" id="*" onclick="jsq(this.id)" value="*" class="buttons">
<input type="button" id="/" onclick="jsq(this.id)" value="/" class="buttons" style="margin-right:0px">
<!--第三排-->
<input type="button" id="1" onclick="jsq(this.id)" value="1" class="buttons">
<input type="button" id="2" onclick="jsq(this.id)" value="2" class="buttons">
<input type="button" id="3" onclick="jsq(this.id)" value="3" class="buttons">
<input type="button" id="+" onclick="jsq(this.id)" value="+" class="buttons">
<input type="button" id="-" onclick="jsq(this.id)" value="-" class="buttons" style="margin-right:0px">
<!--第四排-->
<input type="button" id="0" onclick="jsq(this.id)" value="0" class="buttons">
<input type="button" id="00" onclick="jsq(this.id)" value="00" class="buttons">
<input type="button" id="." onclick="jsq(this.id)" value="." class="buttons">
<input type="button" id="%" onclick="jsq(this.id)" value="%" class="buttons">
<input type="button" id="eva" onclick="eva()" value="=" class="buttons" style="margin-right:0px">
</div>
<div class="footer">
<span class="aside">歡迎使用JavaScript計(jì)算器</span>
<span class="link">
<a href="#" title="聲明" target="_blank">反饋</a>
</span>
</div>
</div>
</body>
</html>
JS部分:
<script>
var num = 0; // 定義第一個(gè)輸入的數(shù)據(jù)
function jsq(num) {
//獲取當(dāng)前輸入
document.getElementById('screenName').value += document.getElementById(num).value;
}
function eva() {
//計(jì)算輸入結(jié)果
document.getElementById("screenName").value = eval(document.getElementById("screenName").value);
}
function clearNum() {
//清0
document.getElementById("screenName").value = null;
document.getElementById("screenName").focus();
}
function tuiGe() {
//退格
var arr = document.getElementById("screenName");
arr.value = arr.value.substring(0, arr.value.length - 1);
}
function onLoad(){
//加載完畢后光標(biāo)自動(dòng)對(duì)應(yīng)到輸入框
document.getElementById("screenName").focus();
}
</script>
CSS部分:
/*Basic reset*/
*{
margin:0;
padding:0;
box-sizing: border-box;
font: 14px Arial,sans-serif;
}
html{
height:100%;
background-color:lightslategrey;
}
#calculator{
margin: 15px auto;
width:330px;
height:400px;
border: 1px solid lightgray;
background-color:darkgrey;
padding:15px;
}
/*LOGO*/
.LOGO{
height:20px;
}
.LOGO .name{
float:left;
line-height:30px;
}
.LOGO .verson{
float:right;
line-height:30px;
}
/*screen*/
#shuRu{
margin-top:15px;
}
.screen{
margin-top:5px;
width:300px;
height:40px;
text-align: right;
padding-right:10px;
font-size:20px;
}
#keys{
border:1px solid lightgray;
height:223px;
margin-top:25px;
padding:8px;
}
#keys .last{
margin-right:0px;
}
.footer{
margin-top:20px;
height:20px;
}
.footer .link{
float:right;
}
#keys .buttons{
float:left;
width: 42px;
height: 36px;
text-align:center;
background-color:lightgray;
margin: 0 17px 20px 0;
}
接下來再給大家分享一則實(shí)例
<html>
<head>
<title>Untitled</title>
<style type="text/css">
input[type=button]
{
width:25px;
height:25px;
}
</style>
<script type="text/javascript">
var result=0;
var point=false; //判斷是小數(shù)點(diǎn)前還是后
var multiple=0.1; //初始化小數(shù)點(diǎn)后的倍率
var sort=0; //標(biāo)記加減乘除
var temp=0; //記錄上一輪結(jié)果
function onbutton0() //數(shù)字按鈕動(dòng)作
{
var name=document.getElementByIdx_x("show");
if(!point) result=10*result+0; //小數(shù)點(diǎn)前的處理
else //小數(shù)點(diǎn)后的處理
{
result=result+multiple*0;
multiple=multiple*0.1;
}
name.value=String(result);
}
function onbutton1()
{
var name=document.getElementByIdx_x("show");
if(!point) result=10*result+1;
else
{
result=result+multiple*1;
multiple=multiple*0.1;
}
name.value=String(result);
}
function onbutton2()
{
var name=document.getElementByIdx_x("show");
if(!point) result=10*result+2;
else
{
result=result+multiple*2;
multiple=multiple*0.1;
}
name.value=String(result);
}
function onbutton3()
{
var name=document.getElementByIdx_x("show");
if(!point) result=10*result+3;
else
{
result=result+multiple*3;
multiple=multiple*0.1;
}
name.value=String(result);
}
function onbutton4()
{
var name=document.getElementByIdx_x("show");
if(!point) result=10*result+4;
else
{
result=result+multiple*4;
multiple=multiple*0.1;
}
name.value=String(result);
}
function onbutton5()
{
var name=document.getElementByIdx_x("show");
if(!point) result=10*result+5;
else
{
result=result+multiple*5;
multiple=multiple*0.1;
}
name.value=String(result);
}
function onbutton6()
{
var name=document.getElementByIdx_x("show");
if(!point) result=10*result+6;
else
{
result=result+multiple*6;
multiple=multiple*0.1;
}
name.value=String(result);
}
function onbutton7()
{
var name=document.getElementByIdx_x("show");
if(!point) result=10*result+7;
else
{
result=result+multiple*7;
multiple=multiple*0.1;
}
name.value=String(result);
}
function onbutton8()
{
var name=document.getElementByIdx_x("show");
if(!point) result=10*result+8;
else
{
result=result+multiple*8;
multiple=multiple*0.1;
}
name.value=String(result);
}
function onbutton9()
{
var name=document.getElementByIdx_x("show");
if(!point) result=10*result+9;
else
{
result=result+multiple*9;
multiple=multiple*0.1;
}
name.value=String(result);
}
function onbutton_point() //小數(shù)點(diǎn)按鈕
{
var name=document.getElementByIdx_x("show");
point=true;
name.value=String(result)+".";
}
function onbutton_back() //退格按鈕
{
var name=document.getElementByIdx_x("show");
var temp=name.value;
name.value="";
for(var i=0;i<temp.length-1;i++)
name.value+=temp[i];
}
function onbutton_plus()
{
var name=document.getElementByIdx_x("show");
point=false;
multiple=0.1;
switch (sort) //遇到下一個(gè)操作符時(shí)判斷上一個(gè)操作符是什么,并進(jìn)行上一個(gè)操作符的計(jì)算
{ //eg:1+2+3= 遇到第二個(gè)+時(shí)判斷前一個(gè)符號(hào),發(fā)現(xiàn)是+則進(jìn)行+運(yùn)算,得到3賦給result
case 1:result=result+temp;
break;
case 2:result=temp-result;
break;
case 3:result=temp*result;
break;
case 4:result=temp/result;
break;
}
sort=1;
temp=result;
result=0;
name.value="0";
}
function onbutton_minus()
{
var name=document.getElementByIdx_x("show");
point=false;
multiple=0.1;
switch (sort)
{
case 1:result=result+temp;
break;
case 2:result=temp-result;
break;
case 3:result=temp*result;
break;
case 4:result=temp/result;
break;
}
sort=2;
temp=result;
result=0;
name.value="0";
}
function onbutton_mult()
{
var name=document.getElementByIdx_x("show");
point=false;
multiple=0.1;
switch (sort)
{
case 1:result=result+temp;
break;
case 2:result=temp-result;
break;
case 3:result=temp*result;
break;
case 4:result=temp/result;
break;
}
sort=3;
temp=result;
result=0;
name.value="0";
}
function onbutton_div()
{
var name=document.getElementByIdx_x("show");
point=false;
multiple=0.1;
switch (sort)
{
case 1:result=result+temp;
break;
case 2:result=temp-result;
break;
case 3:result=temp*result;
break;
case 4:result=temp/result;
break;
}
sort=4;
temp=result;
result=0;
name.value="0";
}
function onbutton_equal()
{
var name=document.getElementByIdx_x("show");
point=false; //重置
multiple=0.1; //重置
switch (sort)
{
case 1:result=result+temp;
break;
case 2:result=temp-result;
break;
case 3:result=temp*result;
break;
case 4:result=temp/result;
break;
}
sort=0; //重置
temp=result;
result=0;
name.value=String(temp);
}
function onbutton_clear() //重置為初始狀態(tài)
{
result=0;
point=false;
multiple=0.1; //初始化小數(shù)點(diǎn)后的倍率
sort=0; //標(biāo)記加減乘除
temp=0; //記錄上一輪結(jié)果
var name=document.getElementByIdx_x("show");
name.value="0";
}
</script>
</head>
<body>
<table align="center">
<tr>
<td colspan="5"><input type="text" value="0" id="show"></td>
</tr>
<tr>
<td><input type="button" value="7" onclick="onbutton7();"></td>
<td><input type="button" value="8" onclick="onbutton8();"></td>
<td><input type="button" value="9" onclick="onbutton9();"></td>
<td><input type="button" value="/" onclick="onbutton_div();"></td>
<td><input type="button" value="<" onclick="onbutton_back();"></td>
</tr>
<tr>
<td><input type="button" value="4" onclick="onbutton4();"></td>
<td><input type="button" value="5" onclick="onbutton5();"></td>
<td><input type="button" value="6" onclick="onbutton6();"></td>
<td><input type="button" value="*" onclick="onbutton_mult();"></td>
<td><input type="button" value="C" onclick="onbutton_clear();"></td>
</tr>
<tr>
<td><input type="button" value="1" onclick="onbutton1();"></td>
<td><input type="button" value="2" onclick="onbutton2();"></td>
<td><input type="button" value="3" onclick="onbutton3();"></td>
<td><input type="button" value="-" onclick="onbutton_minus();"></td>
<td rowspan="2"><input type="button" value="=" style="width:25px;height:57px;" onclick="onbutton_equal();"></td>
</tr>
<tr>
<td colspan="2"><input type="button" value="0" style="width:57px;height:25px;" onclick="onbutton0();"></td>
<td><input type="button" value="." onclick="onbutton_point();"></td>
<td><input type="button" value="+" onclick="onbutton_plus();"></td>
</tr>
</table>
</body>
</html>
- javascript寫的簡單的計(jì)算器,內(nèi)容很多,方法實(shí)用,推薦
- 簡易js代碼實(shí)現(xiàn)計(jì)算器操作
- html+js實(shí)現(xiàn)簡單的計(jì)算器代碼(加減乘除)
- js實(shí)現(xiàn)一個(gè)簡易計(jì)算器
- 用JS寫的簡單的計(jì)算器實(shí)現(xiàn)代碼
- javascript-簡單的計(jì)算器實(shí)現(xiàn)步驟分解(附圖)
- 純javascript代碼實(shí)現(xiàn)計(jì)算器功能(三種方法)
- Vue.js實(shí)現(xiàn)的計(jì)算器功能完整示例
- js網(wǎng)頁版計(jì)算器的簡單實(shí)現(xiàn)
- 基于JavaScript的簡易計(jì)算器功能
相關(guān)文章
基于JS實(shí)現(xiàn)十種酷炫的網(wǎng)頁特效
之前喜歡收集能美化網(wǎng)頁的代碼,比如給網(wǎng)頁加個(gè)背景啦,給鼠標(biāo)加個(gè)特效啦,來來回回也收集到了一些“使用簡單”,“效果爆炸”的頁面,快來學(xué)習(xí)一下吧2022-04-04
JavaSctit 利用FileReader和濾鏡上傳圖片預(yù)覽功能
FileReader 對(duì)象允許Web應(yīng)用程序異步讀取存儲(chǔ)在用戶計(jì)算機(jī)上的文件內(nèi)容,使用 File或 Blob對(duì)象指定要讀取的文件或數(shù)據(jù)。下面通過本文給大家分享JavaSctit 利用FileReader和濾鏡上傳圖片預(yù)覽功能,需要的朋友參考下吧2017-09-09
實(shí)現(xiàn)兩個(gè)文本框同時(shí)輸入的實(shí)例
下面小編就為大家?guī)硪黄獙?shí)現(xiàn)兩個(gè)文本框同時(shí)輸入的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
js操作table中tr的順序?qū)崿F(xiàn)上移下移一行的效果
這篇文章主要介紹了js操作table中tr的順序?qū)崿F(xiàn)上移下移一行的效果 ,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11
JavaScript編程開發(fā)中的五個(gè)實(shí)用小技巧
下面的5點(diǎn)說明確實(shí)不錯(cuò),提高性能與可讀性,大家可以根據(jù)需要選擇使用。2010-07-07

