我的意思是 计算器 进行加减乘除 按数字比如123456789 我只要求按出 5位数 按12345后面的就按不出来了 不能超过5位数 后面再按就限制 这样怎么做 如何写这样的代码
求大神告知
function addNum(n) {
var nowNum = myform.num.value;
if (nowNum == "0") {
myform.num.value = n
} else if(myform.num.value.length<5) {/////////
myform.num.value = myform.num.value + n;
}
}
JiSuanQi
<br> var oper = ""<br> var num = 0;<br> //输入数字<br> function addNum(n){<br> var nowNum = myform.num.value;<br> if(nowNum == "0"){<br> myform.num.value = n<br> }else{<br> myform.num.value = myform.num.value + n;<br> }<br> }<br> //添加运算符<br> function setOper(op){<br> oper = op;<br> num = myform.num.value*1;<br> }<br> function getCount(){<br> if(oper != ""){<br> if(oper == "+"){</p> <pre><code> }else if(oper == "-"){ }else if(oper == "*"){ }else if(oper == "/"){ }else if(oper == "%"){ } } } </script> <style> #ceng{width:600px;height:650px;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')} </style> </head> <body bgcolor="green"> <center> <form id="myform"> <div id="ceng" align="center"><br><br><font size="7">简易计算器</font><br><br> <table border="1" width="560px" height="480px" 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="addNum()"/> </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="addNum()"/> </td> </tr> <tr> <td> <input id="bt0" type="button" value="0" onclick="addNum(0)"/> </td> <td> <input id="bt13" type="button" value="." onclick="addNum()"/> </td> <td> <input id="bt14" type="button" value="%" onclick="addNum(1)"/> </td> <td> <input id="bt15" type="button" value="/" onclick="addNum()"/> </td> </tr> <tr> <td> <input id="bt16" type="button" value="C" onclick="addNum()"/> </td> <td> <input id="bt17" type="button" value="←" onclick="addNum()"/> </td> <td colspan="2"> <input id="bt18" type="button" value="=" onclick="addNum()"/> </td> </tr> </table> </div> </form> </center> </body> <script> for (var i = 0;i<19 ;i++ ) { document.getElementById("bt"+i).style.width="140px"; document.getElementById("bt"+i).style.height="90px"; document.getElementById("bt"+i).style.fontSize="80px" document.getElementById("bt"+i).className= "mr"//默认状态按键颜色 document.getElementById("bt"+i).onmouseover = function(){this.className= "jr"};//匿名函数(鼠标进入事件) document.getElementById("bt"+i).onmouseout = function(){this.className= "mr"};//鼠标移出事件(默认事件) document.getElementById("bt"+i).onmousedown = function(){this.className= "ax"};//鼠标按下事件 document.getElementById("bt"+i).onmouseup = function(){this.className= "fk"};//鼠标放开事件 } document.getElementById("bt18").style.width="286px"; document.getElementById("num").style.width="580px"; document.getElementById("num").style.height="90px"; document.getElementById("num").style.fontSize="70px"; </script> </code></pre> <p></html></p>
var len = str.length;
if(len>5){
alert("长度5了");
};
设置一个全局计数器,在keydown事件中,累加