javascript简单计算器

来源:互联网 发布:网络施工队 编辑:程序博客网 时间:2024/06/16 17:34

@CHARSET "UTF-8";body {background-color: #CCE8CF;}
<!DOCTYPE html><html><head><meta charset="UTF-8"><!-- meta标签很有用,有利于搜索引擎SEO的抓取 --><meta http-equiv="description" content="JavaScript计算器,雪豹软件工作室"/><!-- title标签也比较重要,有利于搜索引擎SEO的抓取,title中不要出现没有意义的描述 --><title>JavaScript版计算器,雪豹软件工作室</title> <link rel="stylesheet" type="text/css" href="body.css"><style type="text/css">/*#FF3030#3CB371#006400#00008B*/input[type=button] {    background-color: #FF3030;    border-radius: 4px;    border: none;    padding: 6px 8px;    color: #fff;    font-size: 12px;    cursor: pointer;    margin: 0px;    line-height: normal;    box-sizing: border-box;    display: inline-block;    text-align: center;    vertical-align: middle;}</style><script type="text/javascript">/*调试JavaScript代码的话,可以在火狐浏览器中安装FireBug插件,Firebug是个功能强大的调试工具注意点:Firebug在Firefox浏览器的49.0和 49.0.2版本中支持良好,Firebug在 Firefox浏览器的更高的版本中会有问题,所以最好是在Firefox浏览器的49.0和 49.0.2版本中安装Firebug插件。如果你的Firefox浏览器版本已经是50版本甚至53版本的了,而你又想用Firebug插 件的话,请你还是老老实实的安装回Firefox浏览器的49.0和 49.0.2版本的吧!安装完之后记得在浏览器的选项中把Firefox的自动更新关闭吧!要不然Firefox又会自动更新成最高版本的了!*//* Firebug调试javascript代码的时候有3个快捷键 F8 执行结束或者进入下个断点 F10 单步执行 F11 进入方法*/function getResult(type){//alert(isNaN(" "));if(checkData(type) == false){return;}//alert(parseFloat('3f34wer') + "-my");var number1 = parseFloat(document.jisuanqi.number1.value);var number2 = parseFloat(document.jisuanqi.number2.value);var result = 0;switch (type) {case '+':result = number1 + number2;break;case '-':result = number1 - number2;break;case '*':result = number1 * number2;break;case '/':result = number1 / number2;break;case '%':result = number1 % number2;break;}document.jisuanqi.result.value = result;}function checkData(type){if (document.jisuanqi.number1.value == "") {//还有输入空格的情况没有考虑到alert("第一个操作数不能为空");return false;}if (document.jisuanqi.number2.value == "") {//还有输入空格的情况没有考虑到alert("第二个操作数不能为空");return false;}if (isNaN(document.jisuanqi.number1.value)) {//还有输入空格的情况没有考虑到alert("第一个操作数不是数字");return false;}if (isNaN(document.jisuanqi.number2.value)) {//还有输入空格的情况没有考虑到alert("第二个操作数不是数字");return false;}if (('%' == type || '/' == type) && (document.jisuanqi.number2.value == 0)) {alert("除数不能为0!");return false;}return true;}</script></head><body><div align="center"><form name="jisuanqi">数字1:<input type="text" name="number1"><br><br>数字2:<input type="text" name="number2"><br><br>结果:<input type="text" name="result" readonly="readonly"><br><br><input type="button" value="<+加>" onclick="getResult('+')"><input type="button" value="<-减>" onclick="getResult('-')"><input type="button" value="<×乘>" onclick="getResult('*')"><input type="button" value="<÷除>" onclick="getResult('/')"><input type="button" value="<%取余数>" onclick="getResult('%')"></form></div></body></html>



0 0
原创粉丝点击