色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

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

javascript實(shí)現(xiàn)計(jì)算器功能

瀏覽:2日期:2023-06-22 16:49:29

本文實(shí)例為大家分享了javascript實(shí)現(xiàn)計(jì)算器功能的具體代碼,供大家參考,具體內(nèi)容如下

javascript實(shí)現(xiàn)計(jì)算器功能

問題描述:

1、除法操作時(shí),如果被除數(shù)為0,則結(jié)果為02、結(jié)果如果為小數(shù),最多保留小數(shù)點(diǎn)后兩位,如2 / 3 =0.67(顯示0.67),1 / 2 = 0.5(顯示0.5)

<!DOCTYPE html><html> <head> <meta charset='utf-8'> <title>百度筆試0329</title> <style type='text/css'> body, ul, li,select { margin: 0; padding: 0; box-sizing: border-box; } ul,li {list-style: none;} .calculator { max-width: 300px; margin: 20px auto; border: 1px solid #eee; border-radius: 3px; } .cal-header { font-size: 16px; color: #333; font-weight: bold; height: 48px; line-height: 48px; border-bottom: 1px solid #eee; text-align: center; } .cal-main { font-size: 14px; } .cal-main .origin-value { padding: 15px; height: 40px; line-height: 40px; font-size: 20px; text-align: right; overflow: hidden; text-overflow:ellipsis; white-space: nowrap; } .cal-main .origin-type, .cal-main .target-type { padding-left: 5px; width: 70px; font-size: 14px; height: 30px; border: 1px solid #eee; background-color: #fff; vertical-align: middle; margin-right: 10px; border-radius: 3px; } .cal-keyboard { overflow: hidden; } .cal-items { overflow: hidden; } .cal-items li { user-select: none; float: left; display: inline-block; width: 75px; height: 75px; text-align: center; line-height: 75px; border-top: 1px solid #eee; border-left: 1px solid #eee; box-sizing: border-box; } li:nth-of-type(4n+1) { border-left: none; } li[data-action=operator] { background: #f5923e; color: #fff; } .buttons { float: left; width: 75px; } .buttons .btn { width: 75px; background-color: #fff; border-top: 1px solid #eee; border-left: 1px solid #eee; height: 150px; line-height: 150px; text-align: center; } .btn-esc { color: #ff5a34; } .btn-backspace { position: relative; } </style> </head> <body> <div class='calculator'> <header class='cal-header'>簡易計(jì)算器</header> <main class='cal-main'> <div class='origin-value'>0</div> <div class='cal-keyboard'> <ul class='cal-items'> <li data-action='num'>7</li> <li data-action='num'>8</li> <li data-action='num'>9</li> <li data-action='operator'>÷</li> <li data-action='num'>4</li> <li data-action='num'>5</li> <li data-action='num'>6</li> <li data-action='operator'>x</li> <li data-action='num'>1</li> <li data-action='num'>2</li> <li data-action='num'>3</li> <li data-action='operator'>-</li> <li data-action='num'>0</li> <li data-action='operator'>清空</li> <li data-action='operator'>=</li> <li data-action='operator'>+</li> </ul> </div> </main> </div> <script type='text/javascript'> var Calculator = { init: function () { var that = this; if (!that.isInited) { that.isInited = true; // 保存操作信息 // total: Number, 總的結(jié)果 // next: String, 下一個(gè)和 total 進(jìn)行運(yùn)算的數(shù)據(jù) // action: String, 操作符號(hào) that.data = {total: 0, next: ’’, action: ’’}; that.bindEvent(); } }, bindEvent: function () { var that = this; // 請(qǐng)補(bǔ)充代碼:獲取 .cal-keyboard 元素 var keyboardEl = document.getElementsByClassName(’cal-keyboard’)[0] keyboardEl && keyboardEl.addEventListener(’click’, function (event) { // 請(qǐng)補(bǔ)充代碼:獲取當(dāng)前點(diǎn)擊的dom元素 var target = event.target; // 請(qǐng)補(bǔ)充代碼:獲取target的 data-action 值 var action = target.getAttribute(’data-action’); // 請(qǐng)補(bǔ)充代碼:獲取target的內(nèi)容 var value = target.innerHTML; if (action === ’num’ || action === ’operator’) { that.result(value, action === ’num’); } }); }, result: function (action, isNum) { var that = this; var data = that.data; if (isNum) { data.next = data.next === ’0’ ? action : (data.next + action); !data.action && (data.total = 0); } else if (action === ’清空’) { // 請(qǐng)補(bǔ)充代碼:設(shè)置清空時(shí)的對(duì)應(yīng)狀態(tài) data.total = 0; data.next = ’’; data.action = ’’; } else if (action === ’=’) { if (data.next || data.action) { data.total = that.calculate(data.total, data.next, data.action); data.next = ’’; data.action = ’’; } } else if (!data.next) { data.action = action; } else if (data.action) { data.total = that.calculate(data.total, data.next, data.action); data.next = ’’; data.action = action; } else { data.total = +data.next || 0; data.next = ’’; data.action = action; } // ���補(bǔ)充代碼:獲取 .origin-value 元素 var valEl = document.getElementsByClassName(’origin-value’)[0]; valEl && (valEl.innerHTML = data.next || data.total || ’0’); }, calculate: function (n1, n2, operator) { n1 = +n1 || 0; n2 = +n2 || 0; if (operator === ’÷’) { // 請(qǐng)補(bǔ)充代碼:獲取除法的結(jié)果 if(n2 == 0 || n1 == 0) return 0 return Math.round((n1/n2)*100)/100; } else if (operator === ’x’) { // 請(qǐng)補(bǔ)充代碼:獲取乘法的結(jié)果 return n1 * n2; } else if (operator === ’+’) { // 請(qǐng)補(bǔ)充代碼:獲取加法的結(jié)果 return n1 + n2; } else if (operator === ’-’) { // 請(qǐng)補(bǔ)充代碼:獲取減法的結(jié)果 return n1 - n2; } } }; Calculator.init(); </script> </body></html>

更多計(jì)算器功能實(shí)現(xiàn),請(qǐng)點(diǎn)擊專題: 計(jì)算器功能匯總 進(jìn)行學(xué)習(xí)

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

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 午夜视频在线观看一区二区 | 九九国产精品 | 毛片免费视频 | 国产精品一区二区三区四区五区 | 国产精品久久久久久久久久久威 | aaa免费看 | 91精品啪在线看国产网站 | 国产视频中文字幕 | 高清国产一区二区三区 | 91天堂网 | 国产成人亚洲综合 | 国产在线a | 欧洲性大片xxxxx久久久 | 韩国毛片在线观看 | 一级片在线免费看 | 久久综合九色综合欧洲色 | 国产精品线在线精品国语 | 尤蜜网站在线进入免费 | 永久在线| 久久国产国内精品对话对白 | 精品久久成人免费第三区 | 国产在线视频专区 | 亚洲在成人网在线看 | 成人国产精品一级毛片天堂 | 国产呦精品一区二区三区网站 | 97在线播放视频 | 欧美成人一区二区 | 精品国产理论在线观看不卡 | mm在线精品视频 | 午夜丝袜美腿福利视频在线看 | 欧美另类视频在线观看 | 欧美日韩一区二区三区在线播放 | 久久久一区二区三区不卡 | 欧美另类69xxxxx视频 | 毛片女 | 国产成人免费a在线资源 | 欧美一级毛片欧美一级无片 | 国产一区二区三区四区在线观看 | 日本免费不卡在线一区二区三区 | 欧美白人和黑人xxxx猛交视频 | 久久精品国内一区二区三区 |