JS快速實(shí)現(xiàn)簡單計(jì)算器
本文實(shí)例為大家分享了JS實(shí)現(xiàn)簡單計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下
直接上圖
HTML部分
<!DOCTYPE html><html> <head> <meta charset='UTF-8'> <title>我的第一個(gè)計(jì)算器</title> <link rel='stylesheet' href='http://www.cgvv.com.cn/bcjs/計(jì)算器.css' rel='external nofollow' /> <script src='http://www.cgvv.com.cn/bcjs/計(jì)算器.js'> </script> </head> <body> <h1 style='text-align: center;'>歡迎使用</h1> <!--<audio src='http://www.cgvv.com.cn/bcjs/audio/1.mp3' id='music'> </audio>--> <hr /> <p style='text-align: center;color:#6495ED;'>輸入不要超過8位數(shù)哦</p> <div class='box'> <div class='header'> <div class='screen'><b id='s1'></b></div> <div class='screen'><b id='s2'></b></div> </div> <div class='content'> <input type='button' name='' value='開始' /> <input type='button' name='' value='清零' /> <input type='button' name='' value='刪除' /> <input type='button' name='' value='關(guān)閉' /> <input type='button' name='' value='1' /> <input type='button' name='' value='2' /> <input type='button' name='' value='3' /> <input type='button' name='' value='+' /> <input type='button' name='' value='4' /> <input type='button' name='' value='5' /> <input type='button' name='' value='6' /> <input type='button' name='' value='-' /> <input type='button' name='' value='7' /> <input type='button' name='' value='8' /> <input type='button' name='' value='9' /> <input type='button' name='' value='*' /> <input type='button' name='' value='0' /> <input type='button' name='' value='.' /> <input type='button' name='' value='=' /> <input type='button' name='' value='÷' /> <span style='line-height: 30px;'> 制作人:xxx 監(jiān)督人:xxx </span> </div> </div> </body></html>
CSS部分
* { margin: 0px; padding: 0px;}/*body { background: cornflowerblue;}*/.box { width: 260px; height: 330px; border: 5px solid cornflowerblue; margin: 60px auto; border-radius: 10px;}.header { width: 260px; height: 80px; background:lightcyan;}.screen { width: 260px; height: 40px; background:lightcyan; font-size: 25px; text-align: center;}.content { width: 260px; height: 250px; color: yellowgreen; background: #FAEBD7; text-align: center;}input { text-align: center; width: 60px; height: 40px; background: pink; margin-top: 4px; font-size: 15px;}b { color: fuchsia;}
JS部分
window.onload = function() { function $(id) { return document.getElementById(id); } function put1(id){ $(id).onclick = function() { $('s1').innerHTML = $('s1').innerHTML+this.value; if ($('s1').innerHTML==116) { prompt('你愛我嗎'); $('s1').innerHTML = '嘻嘻' } if ($('s1').innerHTML==1129) { alert('我愛你'); } if ($('s1').innerHTML==1314520) { alert('我愛你'); } } } var result; $('b=').onclick = function() { result = eval($('s1').innerHTML); $('s2').innerHTML = '='+result; } $('start').onclick = function() { $('s1').innerHTML = '歡迎使用'; $('s2').innerHTML = '使用前清零'; $('music').play(); } $('clear').onclick = function() { $('s1').innerHTML = ''; $('s2').innerHTML = ''; } $('stop').onclick = function() { $('s1').innerHTML = '再見'; setTimeout(function() { $('s1').innerHTML = ''; $('s2').innerHTML = ''; },3000); $('music').pause(); } $('del').onclick = function() { $('s1').innerHTML = $('s1').innerHTML.substr(0, $('s1').innerHTML.length - 1); } put1('b1'); put1('b2'); put1('b3'); put1('b4'); put1('b5'); put1('b6'); put1('b7'); put1('b8'); put1('b9'); put1('b0'); put1('b+'); put1('b-'); put1('bx'); put1('b÷'); put1('b.');}
更多計(jì)算器功能實(shí)現(xiàn),請(qǐng)點(diǎn)擊專題: 計(jì)算器功能匯總 進(jìn)行學(xué)習(xí)
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. PHP橋接模式Bridge Pattern的優(yōu)點(diǎn)與實(shí)現(xiàn)過程2. asp.net core項(xiàng)目授權(quán)流程詳解3. html中的form不提交(排除)某些input 原創(chuàng)4. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼5. CSS3中Transition屬性詳解以及示例分享6. bootstrap select2 動(dòng)態(tài)從后臺(tái)Ajax動(dòng)態(tài)獲取數(shù)據(jù)的代碼7. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式8. 開發(fā)效率翻倍的Web API使用技巧9. jsp文件下載功能實(shí)現(xiàn)代碼10. ASP常用日期格式化函數(shù) FormatDate()
