JavaScript制作簡單計算器功能
本文實例為大家分享了JavaScript制作簡單計算器的具體代碼,供大家參考,具體內容如下
1. 任務要求
1)掌握數(shù)據的類型轉換
2)學會獲取文本框控件的數(shù)據以及給相應控件賦值
3)掌握有參函數(shù)的定義
4)學會使用數(shù)值判斷函數(shù)
5)學會使用eval()方法編譯字符串算式
2. 需求說明
完成如下圖所示的計算器,在頁面單擊數(shù)字和運算符能實現(xiàn)相應運算,單擊AC可以實現(xiàn)清除屏幕內容。
3. 實現(xiàn)思路
1)在TNumber函數(shù)里實現(xiàn)對輸入的數(shù)據進行累加,以便后面計算結果。
2)同時可以定義一個sum,將點擊的每個按鈕值進行累加,以便最后可以輸出式子。
3)在Clear函數(shù)里實現(xiàn)點擊“AC”按鈕對txt進行清空,以及將sum=“”設為空。
4)在計算結果Calculator函數(shù)里使用eval()函數(shù)計算最終結果。
4.實現(xiàn)思路(JS部分的代碼)
<script type="text/javascript"> ?? ?var sum=""; ?? ?function TNumber(value){ ?? ??? ?document.getElementById("txt").value+=value; ?? ??? ?sum+=value; ?? ?} ?? ?function Clear(){ ?? ??? ?document.getElementById("txt").value=""; ?? ??? ?sum=""; ?? ??? ?sum1=""; ?? ?} ?? ?function Calculator(){ ?? ??? ?document.getElementById("txt").value=sum+"="+eval(sum); ?? ?} </script>
5. 運行結果
6. 全部代碼(其中的圖片需要注意一下)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>計算器</title> ?? ?<style type="text/css"> ?? ??? ?#calculator ?? ??? ?{ ?? ??? ??? ?margin: 0 auto; ?? ??? ??? ?border: #000 ?2px solid; ?? ??? ??? ?border-radius: 15px 15px 15px 15px; ?? ??? ??? ?width: 300px; ?? ??? ??? ?height: 460px; ?? ??? ??? ?background-image: url(img/background5.jpg);//url圖片需要自己設定 ?? ??? ?} ?? ??? ?#hiddentxt ?? ??? ?{ ?? ??? ??? ?height: 100px; ?? ??? ?} ?? ??? ?#txt ?? ??? ?{ ?? ??? ??? ?width: 88%; ?? ??? ??? ?height: 80px; ?? ??? ??? ?border-radius: 12px 12px 12px 12px; ?? ??? ??? ?font-size: 24px; ?? ??? ??? ?font-weight: bold; ?? ??? ??? ?background: #F4F4F4; ?? ??? ??? ?color: #080808; ?? ??? ??? ?border: black 5px solid; ?? ??? ??? ?margin-top: 15px; ?? ??? ??? ?margin-left:12px; ?? ??? ??? ?font-family: "Comic Sans MS", "Leelawadee UI"; ?? ??? ?} ?? ??? ?input[type=button] ?? ??? ?{ ?? ??? ??? ?width: 56px; ?? ??? ??? ?height: 56px; ?? ??? ??? ?margin-top: 10px; ?? ??? ??? ?margin-left: 13px; ?? ??? ??? ?border-radius: 10px; ?? ??? ??? ?font-size: 20px; ?? ??? ??? ?font-weight: bold; ?? ??? ??? ?background: #fff; ?? ??? ??? ?font-family:"Comic Sans MS"; ?? ??? ?} ?? ??? ?#Zero,#AC ?? ??? ?{ ?? ??? ??? ?width: 129px; ?? ??? ?} ?? ??? ?input[type=button]:hover ?? ??? ?{ ?? ??? ??? ?color: #fff; ?? ??? ??? ?background: #000; ?? ??? ??? ?animation-name:mybutton; ?? ??? ??? ?animation-duration: 0.8s; ? ? ? ??? ? ? ?animation-timing-function:ease-in-out; ?? ??? ?} ?? ??? ?@keyframes mybutton ?? ??? ?{ ?? ??? ??? ?0% ?? ??? ??? ?{ ?? ??? ??? ??? ?background: #fff; ?? ??? ??? ??? ?color: #000; ?? ??? ??? ?} ?? ? ?? ??? ??? ?100% ?? ??? ??? ?{ ?? ??? ??? ??? ?background:#000; ?? ??? ??? ??? ?color: #fff; ?? ??? ??? ?} ?? ??? ?} ?? ?</style> </head> ? <body> ?? ?<div id="calculator"> ?? ??? ? ?? ?<input type="text" id="txt" ?readonly><br> ?? ?<input type="button" id="AC" value="AC" onClick="Clear()"> ?? ?<input type="button" ?onClick="TNumber(this.value)" value="/"> ?? ?<input type="button" ?onClick="TNumber(this.value)" value="%"> ?? ?<br> ?? ??? ? ?? ?<input type="button" id="Seven" onClick="TNumber(this.value)" value="7"> ?? ?<input type="button" id="Eight" onClick="TNumber(this.value)" value="8"> ?? ?<input type="button" id="Nine" onClick="TNumber(this.value)" value="9"> ?? ?<input type="button" ?onClick="TNumber(this.value)" value="+"> ?? ?<br> ?? ??? ? ?? ?<input type="button" id="Four" ?onClick="TNumber(this.value)" value="4"> ?? ?<input type="button" id="Five" onClick="TNumber(this.value)" value="5"> ?? ?<input type="button" id="Six" onClick="TNumber(this.value)" value="6"> ?? ?<input type="button" ?onClick="TNumber(this.value)" value="-"> ?? ?<br> ?? ??? ? ?? ?<input type="button" id="One" ?onClick="TNumber(this.value)" value="1" > ?? ?<input type="button" id="Two" ?onClick="TNumber(this.value)" value="2" ?> ?? ?<input type="button" id="Three" onClick="TNumber(this.value)" value="3"> ?? ?<input type="button" ?onClick="TNumber(this.value)" value="*"> ?? ?<br> ? ?? ?<input type="button" id="Zero" onClick="TNumber(this.value)" value="0"> ?? ?<input type="button" id="Dot" onClick="TNumber(this.value)" value="."> ?? ?<input type="button" ?onClick="Calculator()" value="="> ?? ??? ? ?? ?</div> ?? ? ?? ?<script type="text/javascript"> ?? ??? ?var sum=""; ?? ??? ?function TNumber(value){ ?? ??? ??? ?document.getElementById("txt").value+=value; ?? ??? ??? ?sum+=value; ?? ??? ?} ?? ??? ?function Clear(){ ?? ??? ??? ?document.getElementById("txt").value=""; ?? ??? ??? ?sum=""; ?? ??? ??? ?sum1=""; ?? ??? ?} ?? ??? ?function Calculator(){ ?? ??? ??? ?document.getElementById("txt").value=sum+"="+eval(sum); ?? ??? ?} ?? ?</script> </body> </html>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
javascript算法題 求任意一個1-9位不重復的N位數(shù)在該組合中的大小排列序號
從1--9中選取N個數(shù)字,組成不重復的N位數(shù),從小到大進行編號,當輸入其中任何一個數(shù)M時,能找出該數(shù)字對應的編號2012-07-07javascript 復雜的嵌套環(huán)境中輸出單引號和雙引號
如果簡單的嵌套一般都是外面用雙引號,則里面用單引號,反之亦同,如果特別負責的嵌套大家看下如下的方法。2009-05-05WordPress中鼠標懸停顯示和隱藏評論及引用按鈕的實現(xiàn)
這篇文章主要介紹了WordPress中鼠標懸停顯示和隱藏評論及引用按鈕的實現(xiàn),順帶顯示和隱藏評論者信息的實現(xiàn)方法,非常實用,需要的朋友可以參考下2016-01-01