久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

js實(shí)現(xiàn)一個(gè)簡(jiǎn)易的計(jì)算器

瀏覽:126日期:2024-04-02 13:22:38

利用原生js實(shí)現(xiàn)一個(gè)簡(jiǎn)易的計(jì)算器(附詳細(xì)注釋?zhuān)┐蠹覅⒖迹唧w內(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> // 獲取到所有類(lèi)名為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)擊誰(shuí),this就指向誰(shuí),在這里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)算符的類(lèi)型進(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; } }}; }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 梨树县| 通河县| 剑阁县| 黑山县| 河源市| 吉林省| 灵川县| 九龙坡区| 石狮市| 安丘市| 库尔勒市| 宣恩县| 汪清县| 师宗县| 无为县| 竹北市| 武平县| 赤水市| 万全县| 海盐县| 囊谦县| 平谷区| 德兴市| 宜宾市| 车险| 黄骅市| 汉阴县| 中卫市| 南和县| 凉山| 安泽县| 杨浦区| 呼伦贝尔市| 海伦市| 肇源县| 凉山| 许昌县| 济宁市| 改则县| 泰宁县| 古蔺县|