求助:html代码问题,怎么做一个加减乘除的简单计算器?如何创建函数?知道的回一下,谢谢!
就是任意输入2个数,点击加减乘除进行简单计算,对于编写函数有些困难,望高手帮助下,谢谢!QQ:442486626用Javascript定义函数!!!!!!!!...
就是任意输入2个数,点击加减乘除进行简单计算,对于编写函数有些困难,望高手帮助下,谢谢!
QQ:442486626
用Javascript定义函数!!!!!!!! 展开
QQ:442486626
用Javascript定义函数!!!!!!!! 展开
1个回答
展开全部
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>runcode</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function fun(t){
var fa = parseInt(document.getElementById('fa').value);
var fb = parseInt(document.getElementById('fb').value);
var fc = document.getElementById('fc');
if(isNaN(fa) || isNaN(fb)){
alert("输入错误,请重新输入");
return;
}
switch(t){
case 1:
fc.value = fa + fb;
break;
case 2:
fc.value = fa - fb;
break;
case 3:
fc.value = fa * fb;
break;
case 4:
fc.value = fa / fb;
break;
}
}
</script>
</head>
<body>
<input type="text" id="fa" value=""/>
<input type="text" id="fb" value=""/>
<input type="text" id="fc" value=""/>
<input type="button" value="+" onclick="fun(1);" />
<input type="button" value="-" onclick="fun(2);" />
<input type="button" value="x" onclick="fun(3);" />
<input type="button" value="/" onclick="fun(4);" />
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>runcode</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function fun(t){
var fa = parseInt(document.getElementById('fa').value);
var fb = parseInt(document.getElementById('fb').value);
var fc = document.getElementById('fc');
if(isNaN(fa) || isNaN(fb)){
alert("输入错误,请重新输入");
return;
}
switch(t){
case 1:
fc.value = fa + fb;
break;
case 2:
fc.value = fa - fb;
break;
case 3:
fc.value = fa * fb;
break;
case 4:
fc.value = fa / fb;
break;
}
}
</script>
</head>
<body>
<input type="text" id="fa" value=""/>
<input type="text" id="fb" value=""/>
<input type="text" id="fc" value=""/>
<input type="button" value="+" onclick="fun(1);" />
<input type="button" value="-" onclick="fun(2);" />
<input type="button" value="x" onclick="fun(3);" />
<input type="button" value="/" onclick="fun(4);" />
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询