用javascript 制作计算器中遇到的问题
<tablewidth="200"border="1"><tr><tdwidth="45">输入:</td><tdcolspan="3"><inputtype="text...
<table width="200" border="1">
<tr>
<td width="45">输入:</td>
<td colspan="3"><input type="text" value="" id="msg" style="width:130px;"></td>
</tr>
<tr>
<td>结果:</td>
<td colspan="3"><input type="text" value="" id="r" style="width:130px;"></td>
</tr>
<tr>
<td colspan="2"><input type="button" value="C E" id="c" ></td>
<td colspan="2"><input type="button" value="退格" ></td>
</tr>
<tr>
<td><input type="button" value="9"></td>
<td width="39"><input type="button" value="8"></td>
<td width="35"><input type="button" value="7"></td>
<td width="53"><input type="button" value="/"></td>
</tr>
<tr>
<td><input type="button" value="6"></td>
<td><input type="button" value="5"></td>
<td><input type="button" value="4"></td>
<td><input type="button" value="*"></td>
</tr>
<tr>
<td><input type="button" value="3"></td>
<td><input type="button" value="2"></td>
<td><input type="button" value="1"></td>
<td><input type="button" value="-"></td>
</tr>
<tr>
<td><input type="button" value="0"></td>
<td><input type="button" value="."></td>
<td><input type="button" value="=" id="s"></td>
<td><input type="button" value="+"></td>
</tr>
</table>
<script type="text/javascript">
var x=document.getElementsByTagName("input");
for(var f1 in x){
x[f1].onclick = function(){
m(this.value)
}
}
document.getElementById("s").onclick = function(){
var ss = document.getElementById("msg").value;
document.getElementById("r").value = eval(ss);
};
document.getElementById("c").onclick = function(){
document.getElementById("msg").value="";
document.getElementById("r").value="";
}
function m(s){
var ss = document.getElementById("msg").value;
ss = ss || "";
ss = ""+ss + s;
document.getElementById("msg").value= ss;
}
</script>
怎样添加制作 退格键的功能 解释function m(s) 这个函数里面命令的功能和意思 展开
<tr>
<td width="45">输入:</td>
<td colspan="3"><input type="text" value="" id="msg" style="width:130px;"></td>
</tr>
<tr>
<td>结果:</td>
<td colspan="3"><input type="text" value="" id="r" style="width:130px;"></td>
</tr>
<tr>
<td colspan="2"><input type="button" value="C E" id="c" ></td>
<td colspan="2"><input type="button" value="退格" ></td>
</tr>
<tr>
<td><input type="button" value="9"></td>
<td width="39"><input type="button" value="8"></td>
<td width="35"><input type="button" value="7"></td>
<td width="53"><input type="button" value="/"></td>
</tr>
<tr>
<td><input type="button" value="6"></td>
<td><input type="button" value="5"></td>
<td><input type="button" value="4"></td>
<td><input type="button" value="*"></td>
</tr>
<tr>
<td><input type="button" value="3"></td>
<td><input type="button" value="2"></td>
<td><input type="button" value="1"></td>
<td><input type="button" value="-"></td>
</tr>
<tr>
<td><input type="button" value="0"></td>
<td><input type="button" value="."></td>
<td><input type="button" value="=" id="s"></td>
<td><input type="button" value="+"></td>
</tr>
</table>
<script type="text/javascript">
var x=document.getElementsByTagName("input");
for(var f1 in x){
x[f1].onclick = function(){
m(this.value)
}
}
document.getElementById("s").onclick = function(){
var ss = document.getElementById("msg").value;
document.getElementById("r").value = eval(ss);
};
document.getElementById("c").onclick = function(){
document.getElementById("msg").value="";
document.getElementById("r").value="";
}
function m(s){
var ss = document.getElementById("msg").value;
ss = ss || "";
ss = ""+ss + s;
document.getElementById("msg").value= ss;
}
</script>
怎样添加制作 退格键的功能 解释function m(s) 这个函数里面命令的功能和意思 展开
1个回答
展开全部
<input type="button" id="back" value="退格" >
document.getElementById("back").onclick = function(){
var ss = document.getElementById("msg").value;
//替换最后一个字符为空,即为退格
ss = ss.replace(/.$/,'');
document.getElementById("msg").value= ss;
};
//m(s) 函数为追加“输入字符”
function m(s){
var ss = document.getElementById("msg").value;
ss = ss || ""; //没有则为空
ss = ""+ss + s; //追加字符
document.getElementById("msg").value= ss; //设置显示追加字符
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询