JS编写的计算器 如何实现 连续加减乘除四则运算 求大神指点 急帮我再看看小数点的问题 1C
``````<html>
<title>JiSuanQi</title>
<!-- 移动层外部JS文件 -->
<script src="src/js/yidong.js"></script>

<script>
var oper = ""
var isClickOper = false//是否点击了运算符
var num = 0;
var isClickPoint = false;//是否点击了小数点
//输入数字
var nowNum = myform.num.value;
if(nowNum == "0"||isClickOper){//直接赋值
myform.num.value = n;
isClickOper = false;
}else if(myform.num.value.length<9){//限制文本框按键输入数字长度最多9个
//myform.num.value = myform.num.value + n;//连接赋值
myform.num.value += n  //也可以这样写连接赋值
}
}
//添加运算符
function setOper(op){
oper = op;
num = myform.num.value*1;
isClickOper = true;
}

//得到结果
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;
}
}

//添加小数点
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>

<body bgcolor="green">
<center>
<form id="myform">
<div onmousemove="mouseMove()" >
<input type="button" value="打开" onclick="ceng.style.display='block'">
<input type="button" value="关闭" onclick="ceng.style.display='none'">
<div onmousedown="mouseDown()" onmouseup="mouseUp()" id="ceng" style="top:80px;left:300px;position:absolute;display:none;cursor:wait"><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>
</td>
<td>
</td>
<td>
</td>
<td>
<input id="bt10" type="button" value="+" onclick="setOper('+')"/>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
<input id="bt11" type="button" value="-" onclick="setOper('-')"/>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
<input id="bt12" type="button" value="*" onclick="setOper('*')"/>
</td>
</tr>
<tr>
<td>
</td>
<td>
</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>
``````

6个回答

dongjinwang 具体怎么做能 在我代码基础上怎么做 我新手 不怎么会 才学几天
4 年多之前 回复

eval就挺厉害了啊