求JS写的计算器 进行连续加减乘除的代码

我的计算器代码如下 就是不能连续加减乘除 求具体代码 本人新手 求详细点


JiSuanQi
<!-- 移动层外部JS文件 -->

<br> var oper = &quot;&quot;<br> var isClickOper = false//是否点击了运算符<br> var num = 0;<br> var isClickPoint = false;//是否点击了小数点<br> //输入数字 <br> function addNum(n){<br> var nowNum = myform.num.value;<br> if(nowNum == &quot;0&quot;||isClickOper){//直接赋值<br> myform.num.value = n;<br> isClickOper = false;<br> }else if(myform.num.value.length&lt;9){//限制文本框按键输入数字长度最多9个<br> //myform.num.value = myform.num.value + n;//连接赋值<br> myform.num.value += n //也可以这样写连接赋值<br> }<br> }<br> //添加运算符<br> function setOper(op){<br> oper = op;<br> num = myform.num.value*1;<br> isClickOper = true;<br> }</p> <pre><code> //得到结果 function getCount(){ if(oper != &quot;&quot;){ if(oper == &quot;+&quot;){ num = num + myform.num.value*1 }else if(oper == &quot;-&quot;){ num = num - myform.num.value*1 }else if(oper == &quot;*&quot;){ num = num * myform.num.value*1 }else if(oper == &quot;/&quot;){ if(myform.num.value == 0){ num =&quot;除数不能为0&quot;}else{ num = num / myform.num.value*1 } }else if(oper == &quot;%&quot;){ num = num % myform.num.value*1 } myform.num.value = num; } } //添加小数点 function addPoint(){ if(!isClickPoint){//判断是否点击过小数点 myform.num.value += &quot;.&quot;; isClickPoint = true; } } //清楚 function cc(){ myform.num.value = &quot;0&quot;; isClickOper = false; isClickPoint = false; num = 0; oper=&quot;0&quot;; } //删除 function del(){ var nowNum = myform.num.value; if(nowNum.length==1){ myform.num.value = &quot;0&quot;; }else{ myform.num.value = nowNum.substr(0,nowNum.length-1) } } &lt;/script&gt; &lt;!--鼠标进入默认按下放开样式--&gt; &lt;style&gt; #ceng{width:440px;height:500px;background:#323850;} .jr{background-Image:url(&#39;jr.png&#39;)} .mr{background-Image:url(&#39;mr.png&#39;)} .ax{background-Image:url(&#39;ax.png&#39;)} .fk{background-Image:url(&#39;fk.png&#39;)} table{position:relative;top:30px;} &lt;/style&gt; &lt;/head&gt; &lt;body bgcolor=&quot;green&quot;&gt; &lt;center&gt; &lt;form id=&quot;myform&quot;&gt; &lt;div onmousemove=&quot;mouseMove()&quot; &gt; &lt;div onmousedown=&quot;mouseDown()&quot; onmouseup=&quot;mouseUp()&quot; id=&quot;ceng&quot; style=&quot;top:80px;left:300px;position:absolute;cursor:&quot;&gt;&lt;font size=&quot;7&quot;&gt;简易计算器&lt;/font&gt; &lt;table border=&quot;1&quot; width=&quot;422px&quot; height=&quot;360px&quot; align=&quot;center&quot;&gt; &lt;tr&gt; &lt;td colspan=&quot;4&quot;&gt; &lt;input id=&quot;num&quot; type=&quot;text&quot; onfocus=&quot;blur()&quot; value=&quot;0&quot;/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;input id=&quot;bt1&quot; type=&quot;button&quot; value=&quot;1&quot; onclick=&quot;addNum(1)&quot;/&gt; &lt;/td&gt; &lt;td&gt; &lt;input id=&quot;bt2&quot; type=&quot;button&quot; value=&quot;2&quot; onclick=&quot;addNum(2)&quot;/&gt; &lt;/td&gt; &lt;td&gt; &lt;input id=&quot;bt3&quot; type=&quot;button&quot; value=&quot;3&quot; onclick=&quot;addNum(3)&quot;/&gt; &lt;/td&gt; &lt;td&gt; &lt;input id=&quot;bt10&quot; type=&quot;button&quot; value=&quot;+&quot; onclick=&quot;setOper(&#39;+&#39;)&quot;/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;input id=&quot;bt4&quot; type=&quot;button&quot; value=&quot;4&quot; onclick=&quot;addNum(4)&quot;/&gt; &lt;/td&gt; &lt;td&gt; &lt;input id=&quot;bt5&quot; type=&quot;button&quot; value=&quot;5&quot; onclick=&quot;addNum(5)&quot;/&gt; &lt;/td&gt; &lt;td&gt; &lt;input id=&quot;bt6&quot; type=&quot;button&quot; value=&quot;6&quot; onclick=&quot;addNum(6)&quot;/&gt; &lt;/td&gt; &lt;td&gt; &lt;input id=&quot;bt11&quot; type=&quot;button&quot; value=&quot;-&quot; onclick=&quot;setOper(&#39;-&#39;)&quot;/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;input id=&quot;bt7&quot; type=&quot;button&quot; value=&quot;7&quot; onclick=&quot;addNum(7)&quot;/&gt; &lt;/td&gt; &lt;td&gt; &lt;input id=&quot;bt8&quot; type=&quot;button&quot; value=&quot;8&quot; onclick=&quot;addNum(8)&quot;/&gt; &lt;/td&gt; &lt;td&gt; &lt;input id=&quot;bt9&quot; type=&quot;button&quot; value=&quot;9&quot; onclick=&quot;addNum(9)&quot;/&gt; &lt;/td&gt; &lt;td&gt; &lt;input id=&quot;bt12&quot; type=&quot;button&quot; value=&quot;*&quot; onclick=&quot;setOper(&#39;*&#39;)&quot;/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;input id=&quot;bt0&quot; type=&quot;button&quot; value=&quot;0&quot; onclick=&quot;addNum(0)&quot;/&gt; &lt;/td&gt; &lt;td&gt; &lt;input id=&quot;bt13&quot; type=&quot;button&quot; value=&quot;.&quot; onclick=&quot;addPoint()&quot;/&gt; &lt;/td&gt; &lt;td&gt; &lt;input id=&quot;bt14&quot; type=&quot;button&quot; value=&quot;%&quot; onclick=&quot;setOper(&#39;%&#39;)&quot;/&gt; &lt;/td&gt; &lt;td&gt; &lt;input id=&quot;bt15&quot; type=&quot;button&quot; value=&quot;/&quot; onclick=&quot;setOper(&#39;/&#39;)&quot;/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;input id=&quot;bt16&quot; type=&quot;button&quot; value=&quot;C&quot; onclick=&quot;cc()&quot;/&gt; &lt;/td&gt; &lt;td&gt; &lt;input id=&quot;bt17&quot; type=&quot;button&quot; value=&quot;←&quot; onclick=&quot;del()&quot;/&gt; &lt;/td&gt; &lt;td colspan=&quot;2&quot;&gt; &lt;input id=&quot;bt18&quot; type=&quot;button&quot; value=&quot;=&quot; onclick=&quot;getCount()&quot;/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;/div&gt; &lt;/form&gt; &lt;/center&gt; &lt;/body&gt; &lt;script src=&quot;src/js/jsq.js&quot;&gt;&lt;/script&gt; </code></pre> <p></html></p>
查看全部
dongjinwang
dongjinwang
2015/08/07 11:09
  • javascript
  • 点赞
  • 收藏
  • 回答
    私信

1个回复