JavaScript编写简单的计算器
JavaScript编写简单的计算器
发布时间:2016-12-30 来源:查字典编辑
摘要:本文实例讲述了JavaScript编写简单计算器的代码。分享给大家供大家参考。具体如下:运行效果截图如下:具体代码如下计算器/*Basicr...

本文实例讲述了JavaScript编写简单计算器的代码。分享给大家供大家参考。具体如下:

运行效果截图如下:

JavaScript编写简单的计算器1

具体代码如下

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>计算器</title> <style> /*Basic reset*/ *{ margin:0; padding:0; box-sizing: border-box; font: 14px Arial,sans-serif; } html{ height:100%; background-color:lightslategrey; } #calculator{ margin: 15px auto; width:330px; height:400px; border: 1px solid lightgray; background-color:darkgrey; padding:15px; } /*LOGO*/ .LOGO{ height:20px; } .LOGO .name{ float:left; line-height:30px; } .LOGO .verson{ float:right; line-height:30px; } /*screen*/ #shuRu{ margin-top:15px; } .screen{ margin-top:5px; width:300px; height:40px; text-align: right; padding-right:10px; font-size:20px; } #keys{ border:1px solid lightgray; height:223px; margin-top:25px; padding:8px; } #keys .last{ margin-right:0px; } .footer{ margin-top:20px; height:20px; } .footer .link{ float:right; } #keys .buttons{ float:left; width: 42px; height: 36px; text-align:center; background-color:lightgray; margin: 0 17px 20px 0; } </style> </head> <body> <div id="calculator"> <div> <span>简单的计算器</span> <span>@walker</span> </div> <div id="shuRu"> <> <div> <input type="text" id="screenName" name="screenName"> </div> </div> <div id="keys"> <> <> <input type="button" id="7" value="7"> <input type="button" id="8" value="8"> <input type="button" id="9" value="9"> <input type="button" id="Back" value="Back"> <input type="button" id="C" value="C"> <> <input type="button" id="4" value="4"> <input type="button" id="5" value="5"> <input type="button" id="6" value="6"> <input type="button" id="*" value="X"> <input type="button" id="/" value="/"> <> <input type="button" id="1" value="1"> <input type="button" id="2" value="2"> <input type="button" id="3" value="3"> <input type="button" id="+" value="+"> <input type="button" id="-" value="-"> <> <input type="button" id="0" value="0"> <input type="button" id="00" value="00"> <input type="button" id="." value="."> <input type="button" id="%" value="%"> <input type="button" id="eva" value="="> </div> <div> <span>欢迎使用JavaScript计算器</span> <span> <a href="#" title="声明" target="_blank">反馈</a> </span> </div> </div> </body> </html>

js代码:

<script> var num = 0; // 定义第一个输入的数据 function jsq(num) { //获取当前输入 if(num=="%"){ document.getElementById('screenName').value=Math.round(document.getElementById('screenName').value)/100; }else{ document.getElementById('screenName').value += document.getElementById(num).value; } } function eva() { //计算输入结果 document.getElementById("screenName").value = eval(document.getElementById("screenName").value); } function clearNum() { //清0 document.getElementById("screenName").value = null; document.getElementById("screenName").focus(); } function tuiGe() { //退格 var arr = document.getElementById("screenName"); arr.value = arr.value.substring(0, arr.value.length - 1); } </script>

一个简单的计算器就是这样实现的,大家也可以利用javascript编写计算器,,希望对大家的学习有所帮助。

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新Javascript教程学习
热门Javascript教程学习
编程开发子分类