彩世界开奖app官网-彩世界平台官方网址(彩票平台)
做最好的网站
来自 彩世界平台官方网址 2019-11-28 05:58 的文章
当前位置: 彩世界开奖app官网 > 彩世界平台官方网址 > 正文

javascript 简单的计算器【彩世界平台官方网址】

几天前用javascript写了三个简易的总计器,自身测验感到幸好,先给大家观赏下分界面:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="jisuanqi.WebForm1" %>

写给新手:js表单操作

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "; <html xmlns="; <title>简单总计器</title> <head>     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />     <style type="text/css">         body         {             font-size: 12px;             font-family: Arial, 吉优rgia, "Times New Roman" , Times, serif;             color: #555;             text-align: center;             background-color: #e2e2e2;         }         h6         {             margin: 0;             font-size: 12px;         }         #calculator         {             width: 240px;             height: auto;             overflow: hidden;             margin: 10px auto;             border: #fff 1px solid;             padding-bottom: 10px;             background-color: #f2f2f2;         }         #calculator div         {             clear: both;         }         #calculator ul         {             padding: 0;             margin: 5px 14px;             border: #fff 1px solid;             height: auto;             overflow: hidden;         }         #calculator li         {             list-style: none;             float: left;             width: 32px;             height: 32px;             margin: 5px;             display: inline;             line-height: 32px;             font-size: 14px;             background-color: #eaeaea;         }         #calculator li.tool         {             background-color: #e2e2e2;         }         #calculator li:hover         {             background-color: #f9f9f9;             cursor: pointer;         }         #calculator li:active         {             background-color: #fc0;             cursor: pointer;         }         #calculator li.tool:active         {             background-color: #d8e8ff;             cursor: pointer;         }         #calcu-head         {             text-align: left;             padding: 10px 15px 5px;         }         span.imyeah         {             float: right;             color: #ccc;         }         span.imyeah a         {             color: #ccc;         }         .screen         {             width: 200px;             height: 24px;             line-height: 24px;             padding: 4px;             border: #e6e6e6 1px solid;             border-bottom: #f2f2f2 1px solid;             border-right: #f2f2f2 1px solid;             margin: 10px auto;             direction: ltr;             text-align: right;             font-size: 16px;             color: #999;         }         #calcu-foot         {             text-align: left;             padding: 10px 15px 5px;             height: auto;             overflow: hidden;         }         span#note         {             float: left;             width: 210px;             height: auto;             overflow: hidden;             color: red;         }         span.welcome         {             clear: both;             color: #999;         }         span.welcome a         {             float: right;             color: #999;         }     </style>     <script type="text/javascript">         var num = 0, result = 0, numshow = "0";         var operate = 0; //判定输入状态的标记         var calcul = 0; //推断计算状态的声明         var quit = 0; //幸免重复开关的申明        function command(num) {             var str = String(document.calculator.numScreen.value); //取妥贴前来得数据             str = (str != "0") ? ((operate == 0) ? str : "") : ""; //要是当前值不是"0",且状态为0,则赶回当前值,不然重返空值;             str = str String(num); //给当下值增添字符             document.calculator.numScreen.value = str; //刷新展现             operate = 0; //重新恢复生机设置输入状态             quit = 0; //重新载入参数幸免再度按钮的标识         }         function dzero() {             var str = String(document.calculator.numScreen.value);             str = (str != "0") ? ((operate == 0) ? str "00" : "0") : "0"; //若是当前值不是"0",且意况为0,则赶回当str "00",不然再次来到"0";             document.calculator.numScreen.value = str;             operate = 0;         }         function dot() {             var str = String(document.calculator.numScreen.value);             str = (str != "0") ? ((operate == 0) ? str : "0") : "0"; //假如当前值不是"0",且情形为0,则赶回当前值,不然重回"0";             for (i = 0; i <= str.length; i ) { //判定是不是早就有八个点号                 if (str.substr(i, 1) == ".") return false; //倘使有则不再插入             }             str = str ".";             document.calculator.numScreen.value = str;             operate = 0;         }         function del() { //退格             var str = String(document.calculator.numScreen.value);             str = (str != "0") ? str : "";             str = str.substr(0, str.length - 1);             str = (str != "") ? str : "0";             document.calculator.numScreen.value = str;         }         function clearscreen() { //撤消数据             num = 0;             result = 0;             numshow = "0";             document.calculator.numScreen.value = "0";         }         function plus() { //加法             calculate(); //调用计量函数             operate = 1; //改过输入状态             calcul = 1; //改革总计状态为加         }         function minus() { //减法             calculate();             operate = 1;             calcul = 2;         }         function times() { //乘法             calculate();             operate = 1;             calcul = 3;         }         function divide() { //除法             calculate();             operate = 1;             calcul = 4;         }         function persent() { //求余             calculate();             operate = 1;             calcul = 5;         }         function equal() {             calculate(); //等于             operate = 1;             num = 0;             result = 0;             numshow = "0";         }         //         function calculate() {             numshow = Number(document.calculator.numScreen.value);             if (num != 0 && quit != 1) { //决断前一个运算数是或不是为零以致防重复开关的事态                 switch (calcul) { //决断要输入状态                     case 1: result = num numshow; break; //总括" "                     case 2: result = num - numshow; break; //计算"-"                     case 3: result = num * numshow; break;                     case 4: if (numshow != 0) { result = num / numshow; } else { document.getElementById("note").innerHTML = "被除数不可能为零!"; setTimeout(clearnote, 4000) } break;                     case 5: result = num % numshow; break;                 }                 quit = 1; //防止重复按钮             }             else {                 result = numshow;             }             numshow = String(result);             document.calculator.numScreen.value = numshow;             num = result; //存款和储蓄当前值         }         function clearnote() { //清空提醒            document.getElementById("note").innerHTML = "";         }     </script> </head> <body>     <div id="calculator">         <div id="calcu-head">           <center>                 轻巧的计算器</center>         </div>         <form name="calculator" action="" method="get">         <div id="calcu-screen">             <!--配置展现窗口,使用onfocus="this.blur();"制止键盘输入-->             <input type="text" name="numScreen" class="screen" value="0" onfocus="this.blur();" />         </div>         <div id="calcu-btn">             <ul>                 <!--配置开关-->                 <li onclick="command(7)">7</li>                 <li onclick="command(8)">8</li>                 <li onclick="command(9)">9</li>                 <li class="tool" onclick="del()">←</li>                 <li class="tool" onclick="clearscreen()">C</li>                 <li onclick="command(4)">4</li>                 <li onclick="command(5)">5</li>                 <li onclick="command(6)">6</li>                 <li class="tool" onclick="times()">×</li>                 <li class="tool" onclick="divide()">÷</li>                 <li onclick="command(1)">1</li>                 <li onclick="command(2)">2</li>                 <li onclick="command(3)">3</li>                 <li class="tool" onclick="plus()"> </li>                 <li class="tool" onclick="minus()">-</li>                 <li onclick="command(0)">0</li>                 <li onclick="dzero()">00</li>                 <li onclick="dot()">.</li>                 <li class="tool" onclick="persent()">%</li>                 <li class="tool" onclick="equal()">=</li>             </ul>         </div>         <div id="calcu-foot">             <span id="note"></span><span class="welcome">接待使用javascript总结器!</span>         </div>         </form>     </div> </body> </html>

本身不知底写的怎么着,但是对于生手来讲,那自然是风华正茂份大餐,里面能够接触到的东西不菲,能够拿来学习。即使有高手看出在那之中的脱漏、错误等望不吝指教,授予指引。

©I'm Yeah!

分界面就是那般了,不过意义如何呢?

到现在只是个轻易的科班计算器,能实行加减乘除一而再运算,以至求余运算。即便产生被除数为零的失实,上边会付给提醒,就好像那样:

粗略的计算器

PS:这里再为大家推荐七款本站的在线总计器,都以利用js实现,且功效强盛,相信对于贵宗深切理解JavaScript数学生运动算及web设计会持有利于:

在线规范总结器:

在线科学总括器:

上面贴上代码,希望里面包车型大巴笺注丰硕多了。js部分: 复制代码 代码如下: var num=0,result=0,numshow="0"; var operate=0; //判定输入状态的表明 var calcul=0; //剖断计算状态的标记 var quit=0; //幸免重复开关的标记 function command{ var str=String(document.calculator.numScreen.value); //取妥贴前呈现数据 str= ? ? str : "") : ""; //假使当前值不是"0",且景况为0,则赶回当前值,不然重返空值; str=str String; //给当下值扩充字符 document.calculator.numScreen.value=str; //刷新展现 operate=0; //重新复苏设置输入状态 quit=0; //重新恢复生机设置幸免重复开关的标记 } function dzero(){ var str=String(document.calculator.numScreen.value); str= ? ? str "00" : "0") : "0"; //尽管当前值不是"0",且状态为0,则赶回当str "00",不然重回"0"; document.calculator.numScreen.value=str; operate=0; } function dot(){ var str=String(document.calculator.numScreen.value); str= ? ? str : "0") : "0"; //借使当前值不是"0",且状态为0,则赶回当前值,否则重回"0"; for(i=0; i<=str.length;i ){ //判定是或不是早就有一个点号 if return false; //纵然有则不再插入 } str=str "."; document.calculator.numScreen.value=str; operate=0; } function del(){ //退格 var str=String(document.calculator.numScreen.value); str= ? str : ""; str=str.substr; str= ? str : "0"; document.calculator.numScreen.value=str; } function clearscreen(){ //驱除数据 num=0; result=0; numshow="0"; document.calculator.numScreen.value="0"; } function plus; //调用计量函数 operate=1; //改进输入状态 calcul=1; //校订总结状态为加 } function minus; operate=1; calcul=2; } function times; operate=1; calcul=3; } function divide; operate=1; calcul=4; } function persent; operate=1; calcul=5; } function equal; //等于 operate=1; num=0; result=0; numshow="0"; } // function calculate(){ numshow=Number(document.calculator.numScreen.value); if{ //剖断前叁个运算数是或不是为零以致防重复开关的处境 switch{ //推断要输入状态 case 1:result=num numshow;break; //总结" " case 2:result=num-numshow;break; //计算"-" case 3:result=num*numshow;break; case 4:if{result=num/numshow;}else{document.getElementById.innerHTML="被除数不可能为零!"; set提姆eout} break; case 5:result=num%numshow;break; } quit=1; //防止重复开关 } else{ result=numshow; } numshow=String; document.calculator.numScreen.value=numshow; num=result; //存款和储蓄当前值 } function clearnote(){ //清空提醒 document.getElementById.innerHTML=""; } html部分: 复制代码 代码如下:

本文由彩世界开奖app官网发布于彩世界平台官方网址,转载请注明出处:javascript 简单的计算器【彩世界平台官方网址】

关键词: 脚本 简单 计算器 之家 学习心