js實(shí)現(xiàn)一個(gè)簡易的計(jì)算器
利用原生js實(shí)現(xiàn)一個(gè)簡易的計(jì)算器(附詳細(xì)注釋),供大家參考,具體內(nèi)容如下
<!DOCTYPE html><html lang='en'> <head> <meta charset='UTF-8' /> <meta name='viewport' content='width=device-width, initial-scale=1.0' /> <title>Document</title> <style> .divs {width: 500px;height: 600px;border: 1px solid #000000;margin: auto; } .divs > input {width: 460px;height: 45px;margin-left: 18px;margin-top: 10px;font-size: 30px;background-color: white;text-align: right; } .divs > div {width: 100px;height: 100px;float: left;border: 1px solid #000000;margin-left: 18px;margin-top: 25px;font-size: 40px;line-height: 100px;text-align: center;user-select: none; } </style> </head> <body> <div class='divs'> <input type='text' value='0' disabled /> <div class='block'>7</div> <div class='block'>8</div> <div class='block'>9</div> <div class='block'>-</div> <div class='block'>4</div> <div class='block'>5</div> <div class='block'>6</div> <div class='block'>+</div> <div class='block'>1</div> <div class='block'>2</div> <div class='block'>3</div> <div class='block'>*</div> <div class='block'>C</div> <div class='block'>0</div> <div class='block'>=</div> <div class='block'>/</div></div>
js:
<script> // 獲取到所有類名為block的元素 var blocks = document.getElementsByClassName('block'); // 獲取到input var input = document.getElementById('input1'); // 聲明第一個(gè)數(shù)值 var firstValue = 0,bool = false; // 聲明運(yùn)算符 var type; for (var i = 0; i < blocks.length; i++) { //點(diǎn)擊第i個(gè)blockblocks[i].onclick = function () { //點(diǎn)擊誰,this就指向誰,在這里this指向每次點(diǎn)擊的元素 console.log(this); //this.innerHTML顯示點(diǎn)擊的div里面的內(nèi)容(比如1,2,3,-,+) //判斷點(diǎn)擊的為數(shù)字的情況(不是NaN,就是數(shù)字) if (!isNaN(this.innerHTML)) { // bool初始為false,當(dāng)bool為false時(shí),可以不斷輸入,當(dāng)bool為true時(shí),input清空為0 if (bool) { input.value = '0'; bool = false; } // 將input中的value累加點(diǎn)擊的內(nèi)容,將它強(qiáng)轉(zhuǎn)為數(shù)字是為了去掉最前面的0,最后再轉(zhuǎn)為字符 input.value = Number(input.value + this.innerHTML).toString(); } else { //判斷點(diǎn)擊為+ - * /的情況 if (this.innerHTML !== 'C' && this.innerHTML !== '=') { //將第一個(gè)數(shù)存到firstValue firstValue = Number(input.value); //將運(yùn)算符存到type type = this.innerHTML; //將input中的value重置為0 input.value = '0'; } else if (this.innerHTML === 'C') { //判斷點(diǎn)擊C的情況 // 全都重置 firstValue = 0; type = undefined; input.value = '0'; } else { //判斷點(diǎn)擊=的情況 //根據(jù)運(yùn)算符的類型進(jìn)行運(yùn)算 switch (type) {case '+': input.value = (firstValue + Number(input.value)).toString(); break;case '-': input.value = (firstValue - Number(input.value)).toString(); break;case '*': input.value = (firstValue * Number(input.value)).toString(); break;case '/': // 除數(shù)為0時(shí)重置input.value if (Number(input.value) === 0) input.value = '0'; else input.value = (firstValue / Number(input.value)).toString(); break; } //bool為true時(shí),點(diǎn)擊'='后,當(dāng)再次輸入時(shí),input.value為0 bool = true; } }}; }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python excel和yaml文件的讀取封裝2. moment轉(zhuǎn)化時(shí)間戳出現(xiàn)Invalid Date的問題及解決3. python爬蟲實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊4. php實(shí)現(xiàn)當(dāng)前用戶在線人數(shù)5. Android中的緩存6. Android Studio插件7. .net6 在中標(biāo)麒麟下的安裝和部署過程8. Python中內(nèi)建模塊collections如何使用9. Python內(nèi)存映射文件讀寫方式10. java——Byte類/包裝類的使用說明
