JavaScript development calculator JS code
HTML was introduced earlier, now we mainly talk about how to write JS code:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"> <meta name="format-detection" content="telephone=no" /> <title>計(jì)算器</title> <script type="text/javascript" src=""></script> <script> //點(diǎn)擊按鈕執(zhí)行操作 var resultDom = document.getElementById("result"); //var 是定義變量, var operate = true; //定義一個(gè)變量,防止運(yùn)算符的連續(xù)操作 var xop = true; //定義一個(gè)變量,防止小數(shù)點(diǎn)可以連續(xù)輸出 function command(num){ //function 是封裝command if(needclear==1){ needclear=0; resultDom.value=''; } var str = resultDom.value; str =(str =="0"?"":str); resultDom.value = str+num; operate = true; play(num); } //清空 function clearzero(m){ resultDom.value = 0; play(m) } //計(jì)算等號(hào) var needclear=0; function equal(m){ needclear=1; var result = resultDom.value.toString(); var r = eval(result); resultDom.value =r; play(m); } //小數(shù)點(diǎn) function dot(m){ if(xop){ //對(duì)變量進(jìn)行判斷輸出 var num = resultDom.value.toString(); num +="."; resultDom.value = num; xop = ture; } play(m); } //點(diǎn)擊操作符 function tools(op,m){ if(operate){ //對(duì)運(yùn)算符進(jìn)行判斷輸出 var num = resultDom.value; num = (num =="0"?"":num); resultDom.value = num+op; operate = false; } play(m); } //按鍵聲音 function play(num){ var audioDom = document.getElementById("audio"); audioDom.innerHTML = "<embed src='wav/"+num+".wav' width='0' height='0'></embed>" //wav是聲音素材的文件夾名稱。 } </script> </body> </html>
Each function will have comments. Since the voice material cannot be uploaded, you only need to know how to write it. You can download the voice material online and try it yourself.