java web week two
2017-05-22 00:04
141 查看
开发简易计算器
http://www.lai18.com/content/439235.htmlindex.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>计算器</title> <link rel="stylesheet" href="CSS/style.css" type="text/css"> <script type="text/javascript"> let result; //保存点击运算符之前输入框中的数值 let operator; //保存运算符 let isPressEqualsKey = false; //记录是否按下”=“键 //数字键事件 function connectionDigital(control) { var txt = document.getElementById('text'); if (isPressEqualsKey) { txt.value = ""; //已进行过计算,则清空数值输入框重新开始 isPressEqualsKey = false; } if (txt.value.indexOf('.') > -1 && control.value == '.') return false; txt.value += control.value; //将控件值赋给数值输入框中 } //ce键事件:清空数字输入框 function clearAll() { document.getElementById('text').value = ""; result = ""; operator = ""; } // +、-、*、/ 事件 function calculation(control) { operator = control.value; let txt = document.getElementById('text'); if (txt.value == "")return false; //数值输入框中没有数字,则不能输入运算符 result = txt.value; txt.value = ""; } //计算结果 function getResult() { let opValue; let sourseValue = parseFloat(result); let txt = document.getElementById('text'); if (operator == '*') opValue = sourseValue * parseFloat(txt.value); else if (operator == '/') opValue = sourseValue / parseFloat(txt.value); else if (operator == '+') opValue = sourseValue + parseFloat(txt.value); else if (operator == '-') opValue = sourseValue - parseFloat(txt.value); txt.value = opValue; isPressEqualsKey = true; result = ""; opValue = ""; } function percent() { let txt = document.getElementById('text'); if (txt == 0) { document.getElementById('text').value = 0; }else { document.getElementById('text').value /= 100; } } function abs() { let txt = document.getElementById('text'); if (txt.value.indexOf('.') > -1 && control.value == '.') return false; document.getElementById('text').value = -(document.getElementById('text').value) } </script> </head> <body> <div id="jisuanqi"> <table> <caption> <h3>计算器</h3> </caption> <tbody> <tr> <td colspan="4"> <input type="text" id="text" readonly="readonly"/> </td> </tr> <tr> <td> <input type="button" class="buttonGray" id="btnAC" value="AC" onclick="clearAll();"/> </td> <td> <input type="button" class="buttonGray" id="btnabs" value="+/-" onclick="abs();"/> </td> <td> <input type="button" class="buttonGray" id="btnPercent" value="%" onclick="percent();"/> </td> <td> <input type="button" class="buttonYellow" id="btnChu" value="/" onclick="calculation(this);"/> </td> </tr> <tr> <td> <input type="button" class="button" id="btn7" value="7" onclick="connectionDigital(this);"/> </td> <td> <input type="button" class="button" id="btn8" value="8" onclick="connectionDigital(this);"/> </td> <td> <input type="button" class="button" id="btn9" value="9" onclick="connectionDigital(this);"/> </td> <td> <input type="button" class="buttonYellow" id="btnCheng" value="*" onclick="calculation(this);"/> </td> </tr> <tr> <td> <input type="button" class="button" id="btn4" value="4" onclick="connectionDigital(this);"/> </td> <td> <input type="button" class="button" id="btn5" value="5" onclick="connectionDigital(this);"/> </td> <td> <input type="button" class="button" id="btn6" value="6" onclick="connectionDigital(this);"/> </td> <td> <input type="button" class="buttonYellow" id="btnJian" value="-" onclick="calculation(this);"/> </td> </tr> <tr> <td> <input type="button" class="button" id="btn1" value="1" onclick="connectionDigital(this);"/> </td> <td> <input type="button" class="button" id="btn2" value="2" onclick="connectionDigital(this);"/> </td> <td> <input type="button" class="button" id="btn3" value="3" onclick="connectionDigital(this);"/> </td> <td> <input type="button" class="buttonYellow" id="btnPlus" value="+" onclick="calculation(this);"/> </td> </tr> <tr> <td colspan="2"> <input type="button" class="button" id="btn0" value="0" onclick="connectionDigital(this);"/> </td> <td> <input type="button" class="button" id="btnDian" value="." onclick="connectionDigital(this);"/> </td> <td> <input type="button" class="buttonYellow" id="btnEqual" value="=" onclick="getResult();"/> </td> </tr> </tbody> </table> </div> </body> </html>
style.css
*{ margin: 0 auto; } #jisuanqi{ width: 100%; height: 100%; } #text{ width: 99%; height: 100%; text-align: right; border: 1px none; } .button,.buttonGray,.buttonYellow{ width:100%; height: 100%; border: 1px none; } .buttonGray{ background-color: rgba(15, 44, 61, 0.61); } .buttonYellow{ background-color: rgba(255, 155, 38, 0.91); } table,tr,td{ border: 1px solid #BBB; border-collapse: collapse; min-width: 60px; min-height: 60px; } table{ height: 360px; width: 240px; line-height: 40px; } body { background-color: antiquewhite; }
相关文章推荐
- java web week_five 多线程作业
- java web week_one
- java web week_three_one
- 基于JAVA的WEB服务器工作机制(3)完
- java web start实现关键问题(二)
- 基于JAVA的WEB服务器工作机制(1)
- 快速配置你的Java程序到JAWS(Java Web Start)(2)
- JavaServer Faces 技术使Web应用程序开发变得更容易
- 用Java实现Web服务器 HTTP协议
- 使用Java在Web上实现简易干特图之一
- 使用Java在Web上实现简易干特图之三
- 用Java在Web页面上输出统计图 统计报表
- Linux 下 JAVA 的 Web 开发(环境搭建篇)
- JavaServer Faces vs. ASP.NET WebForms
- 使用Java在Web上实现简易干特图之六
- 基于JAVA的WEB服务器工作机制(2)
- Java网络加载协议(JNLP)及Java Web Start
- JAVA WEB快速入门之从编写一个JSP WEB网站了解JSP WEB网站的基本结构、调试、部署
- 对Java Applet和Java Web Start进行数字签名
- Velocity -- Java Web 开发新技术