问题描述
- JS写的简单计算器 求 连续加减乘除代码
-
JS编写的计算器 如何才能实现连续的加减乘除 求大神告知具体方法 代码 我是新手 太复杂的看不懂 谢谢 我的代码如下JiSuanQi
<!-- 移动层外部JS文件 --><br>
var oper = ""<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 == "0"||isClickOper){//直接赋值<br>
myform.num.value = n;<br>
isClickOper = false;<br>
}else if(myform.num.value.length<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 != ""){
if(oper == "+"){
num = num + myform.num.value*1
}else if(oper == "-"){
num = num - myform.num.value*1
}else if(oper == "*"){
num = num * myform.num.value*1
}else if(oper == "/"){
if(myform.num.value == 0){
num ="除数不能为0"}else{
num = num / myform.num.value*1
}
}else if(oper == "%"){
num = num % myform.num.value*1
}
myform.num.value = num;
}
}//添加小数点
function addPoint(){
if(!isClickPoint){//判断是否点击过小数点
myform.num.value += ".";
isClickPoint = true;
}
}
//清楚
function cc(){
myform.num.value = "0";
isClickOper = false;
isClickPoint = false;
num = 0;
oper="0";
}
//删除
function del(){
var nowNum = myform.num.value;
if(nowNum.length==1){
myform.num.value = "0";
}else{
myform.num.value = nowNum.substr(0,nowNum.length-1)
}
}</script>
<!--鼠标进入默认按下放开样式-->
<style>
#ceng{width:440px;height:500px;background:#323850;}
.jr{background-Image:url('jr.png')}
.mr{background-Image:url('mr.png')}
.ax{background-Image:url('ax.png')}
.fk{background-Image:url('fk.png')}
table{position:relative;top:30px;}
</style>
</head><body bgcolor="green">
<center>
<form id="myform">
<div onmousemove="mouseMove()" >
<div onmousedown="mouseDown()" onmouseup="mouseUp()" id="ceng" style="top:80px;left:300px;position:absolute;cursor:"><font size="7">简易计算器</font>
<table border="1" width="422px" height="360px" align="center">
<tr>
<td colspan="4">
<input id="num" type="text" onfocus="blur()" value="0"/>
</td>
</tr>
<tr>
<td>
<input id="bt1" type="button" value="1" onclick="addNum(1)"/>
</td>
<td>
<input id="bt2" type="button" value="2" onclick="addNum(2)"/>
</td>
<td>
<input id="bt3" type="button" value="3" onclick="addNum(3)"/>
</td>
<td>
<input id="bt10" type="button" value="+" onclick="setOper('+')"/>
</td>
</tr>
<tr>
<td>
<input id="bt4" type="button" value="4" onclick="addNum(4)"/>
</td>
<td>
<input id="bt5" type="button" value="5" onclick="addNum(5)"/>
</td>
<td>
<input id="bt6" type="button" value="6" onclick="addNum(6)"/>
</td>
<td>
<input id="bt11" type="button" value="-" onclick="setOper('-')"/>
</td>
</tr>
<tr>
<td>
<input id="bt7" type="button" value="7" onclick="addNum(7)"/>
</td>
<td>
<input id="bt8" type="button" value="8" onclick="addNum(8)"/>
</td>
<td>
<input id="bt9" type="button" value="9" onclick="addNum(9)"/>
</td>
<td>
<input id="bt12" type="button" value="*" onclick="setOper('*')"/>
</td>
</tr>
<tr>
<td>
<input id="bt0" type="button" value="0" onclick="addNum(0)"/>
</td>
<td>
<input id="bt13" type="button" value="." onclick="addPoint()"/>
</td>
<td>
<input id="bt14" type="button" value="%" onclick="setOper('%')"/>
</td>
<td>
<input id="bt15" type="button" value="/" onclick="setOper('/')"/>
</td>
</tr>
<tr>
<td>
<input id="bt16" type="button" value="C" onclick="cc()"/>
</td>
<td>
<input id="bt17" type="button" value="←" onclick="del()"/>
</td>
<td colspan="2">
<input id="bt18" type="button" value="=" onclick="getCount()"/>
</td></tr>
</table>
</div>
</div>
</form>
</center>
</body><script src="src/js/jsq.js"></script>
</code></pre><p></html></p>
解决方案
<!--
function operation(flag)
{
fl = flag;
switch(flag)
{
case "+":
doAdd();break;
case "-":
doMinus();break;
case "*":
doMultiply();break;
case "/":
doDivide();break;
}
}
var num1;
var num2;
var isPass = true;
var result;
var fl;
function getValue()
{
isPass =true;
num1 =document.getElementById("txtNum1").value;
num2 = document.getElementById("txtNum2").value;
result = document.getElementById("txtResult");
checkVal();
}
function checkVal()
{
if(num1=="" || num2=="")
{
alert("不能为空");
isPass = false;
}else
{
if(num1!=0)
{
if(!Number(num1))
{
isPass = false;
}
}
if(fl=="/")
{
if(num2 == 0)
{
alert("除数不能为0");
isPass = false;
}
}
if(fl!="/"){
if(num2!=0){
if(!Number(num2))
{
alert("请输入数字");
isPass = false;
}
}
}
}
}
//加法
function doAdd()
{
getValue();
if(isPass)
{
result.value =Number(num1)+Number(num2);
}
}
//减法
function doMinus()
{
getValue();
if(isPass)
{
result.value =Number(num1)-Number(num2);
}
}
//乘法
function doMultiply(){
getValue();
if(isPass)
{
result.value =Number(num1)*Number(num2);
}
}
//除法
function doDivide(){
getValue();
if(isPass)
{
result.value =Number(num1)/Number(num2);
}
}
function clearValue()
{
document.getElementById("txtNum1").value = "";
document.getElementById("txtNum2").value = "";
document.getElementById("txtResult").value = "";
}
// -->/mce:script