js實作簡單計算器_javascript技巧
參考部分資料,編寫一個簡單的計算器案例,雖然完成了正常需求,但是也有不滿之處,待後續實力提升後再來補充,先把不足之處列出:
1:本來打算只要開啟頁面,計算機的輸入框會顯示一個預設為0的狀態,但是在輸入框加入預設顯示為0的時候,選擇資料輸入時,該0會顯示輸入數字的前面,例如”0123“,由於能力有限,待後續實力提升再來補充完善!
2:目前只能實現滑鼠控制選擇按鈕,待完善鍵盤輸入功能。
3:乘法的那個符號在本來想改成」×「這個符號的,待後續完善。
附圖片一張:
html部分:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>计算器</title> </head> <body onload="onLoad()"> <div id="calculator"> <div class="LOGO"> <span class="name">简单的计算器</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计算器</span> <span class="link"> <a href="#" title="声明" target="_blank">反馈</a> </span> </div> </div> </body> </html>
JS部分:
<script> var num = 0; // 定义第一个输入的数据 function jsq(num) { //获取当前输入 document.getElementById('screenName').value += document.getElementById(num).value; } function eva() { //计算输入结果 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(){ //加载完毕后光标自动对应到输入框 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; }
接下來再給大家分享一則實例
<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; //判断是小数点前还是后 var multiple=0.1; //初始化小数点后的倍率 var sort=0; //标记加减乘除 var temp=0; //记录上一轮结果 function onbutton0() //数字按钮动作 { var name=document.getElementByIdx_x("show"); if(!point) result=10*result+0; //小数点前的处理 else //小数点后的处理 { 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() //小数点按钮 { 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) //遇到下一个操作符时判断上一个操作符是什么,并进行上一个操作符的计算 { //eg:1+2+3= 遇到第二个+时判断前一个符号,发现是+则进行+运算,得到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() //重置为初始状态 { result=0; point=false; multiple=0.1; //初始化小数点后的倍率 sort=0; //标记加减乘除 temp=0; //记录上一轮结果 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>

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

人臉偵測辨識技術已經是一個比較成熟且應用廣泛的技術。而目前最廣泛的網路應用語言非JS莫屬,在Web前端實現人臉偵測辨識相比後端的人臉辨識有優勢也有弱勢。優點包括減少網路互動、即時識別,大大縮短了使用者等待時間,提高了使用者體驗;弱勢是:受到模型大小限制,其中準確率也有限。如何在web端使用js實現人臉偵測呢?為了實現Web端人臉識別,需要熟悉相關的程式語言和技術,如JavaScript、HTML、CSS、WebRTC等。同時也需要掌握相關的電腦視覺和人工智慧技術。值得注意的是,由於Web端的計

如何使用JS和百度地圖實現地圖平移功能百度地圖是一款廣泛使用的地圖服務平台,在Web開發中經常用於展示地理資訊、定位等功能。本文將介紹如何使用JS和百度地圖API實作地圖平移功能,並提供具體的程式碼範例。一、準備工作使用百度地圖API前,首先需要在百度地圖開放平台(http://lbsyun.baidu.com/)上申請一個開發者帳號,並建立一個應用程式。創建完成

如何使用PHP和JS創建股票蠟燭圖股票蠟燭圖是股票市場中常見的技術分析圖形,透過繪製股票的開盤價、收盤價、最高價和最低價等數據,幫助投資者更直觀地了解股票的價格波動情形。本文將教你如何使用PHP和JS創建股票蠟燭圖,並附上具體的程式碼範例。一、準備工作在開始之前,我們需要準備以下環境:1.一台運行PHP的伺服器2.一個支援HTML5和Canvas的瀏覽器3

股票分析必備工具:學習PHP和JS繪製蠟燭圖的步驟,需要具體程式碼範例隨著網路和科技的快速發展,股票交易已成為許多投資者的重要途徑之一。而股票分析是投資人決策的重要一環,其中蠟燭圖被廣泛應用於技術分析。學習如何使用PHP和JS繪製蠟燭圖將為投資者提供更多直觀的信息,幫助他們更好地做出決策。蠟燭圖是一種以蠟燭形狀來展示股票價格的技術圖表。它展示了股票價格的

如何使用JS和百度地圖實現地圖點擊事件處理功能概述:在網路開發中,經常需要使用地圖功能來展示地理位置和地理資訊。而地圖上的點擊事件處理是地圖功能中常用且重要的一環。本文將介紹如何使用JS和百度地圖API來實現地圖的點擊事件處理功能,並給出具體的程式碼範例。步驟:匯入百度地圖的API檔案首先,要在HTML檔案中匯入百度地圖API的文件,可以透過以下程式碼實現:

如何使用JS和百度地圖實現地圖熱力圖功能簡介:隨著互聯網和行動裝置的快速發展,地圖成為了普遍的應用場景。而熱力圖作為一種視覺化的展示方式,能夠幫助我們更直觀地了解數據的分佈。本文將介紹如何使用JS和百度地圖API來實現地圖熱力圖的功能,並提供具體的程式碼範例。準備工作:在開始之前,你需要準備以下事項:一個百度開發者帳號,並建立一個應用,取得到對應的AP

隨著網路金融的快速發展,股票投資已經成為了越來越多人的選擇。而在股票交易中,蠟燭圖是常用的技術分析方法,它能夠顯示股票價格的變動趨勢,幫助投資人做出更精準的決策。本文將透過介紹PHP和JS的開發技巧,帶領讀者了解如何繪製股票蠟燭圖,並提供具體的程式碼範例。一、了解股票蠟燭圖在介紹如何繪製股票蠟燭圖之前,我們首先需要先了解什麼是蠟燭圖。蠟燭圖是由日本人

js和vue的關係:1、JS作為Web開發基石;2、Vue.js作為前端框架的崛起;3、JS與Vue的互補關係;4、JS與Vue的實踐應用。
