设计一个网页,有3个文本框和2个按钮,第一个文本框用于输入一个用于运算的数字,第二个文本框用于输入运算符(如+,-,*,/),第三个文本框用于输入一个用于运算的数字,按下“确定”,就根据第二个文本框的符号和两外两个文本框的数字进行相应的运算,并显示在网页上。新手没分,希望大家帮帮忙,在考试,紧急
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title</title>
<script type="text/javascript">
function $(id){return document.getElementById(id);}
window.onload = function(){
$("btn").onclick=function(){
var n1=$("num1").value;
var n2=$("num2").value;
if($("fun1").value=="+")
$("result1").innerText=Number(n1)+Number(n2);
if($("fun1").value=="-")
$("result1").innerText=Number(n1)-Number(n2);
if($("fun1").value=="*")
$("result1").innerText=Number(n1)*Number(n2);
if($("fun1").value=="/")
$("result1").innerText=Number(n1)/Number(n2);
};
};
</script>
</head>
<body>
<input id="num1" type=text />
<br>
<br>
<input id="fun1" type=text />
<br>
<br>
<input id="num2" type=text />
<br>
<br>
<div>
<input type="button" id="btn" value="do"/>
</div>
<br>
<br>
<span id="result1"></span>
</body>
</html>追问
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title</title>
<script type="text/javascript">
function $(id){return document.getElementById(id);}
window.onload = function(){
$("btn").onclick=function(){
var n1=$("num1").value;
var n2=$("num2").value;
if($("fun1").value=="+")
$("result1").innerText=Number(n1)+Number(n2);
if($("fun1").value=="-")
$("result1").innerText=Number(n1)-Number(n2);
if($("fun1").value=="*")
$("result1").innerText=Number(n1)*Number(n2);
if($("fun1").value=="/")
if($("num2").value==0)
{
alert("除数不能为0!")//如果除数为零,添加报错!
}
else{
$("result1").innerText=Number(n1)/Number(n2);//除数不为零,正常运算~
}
};
};
</script>
</head>
<body>
<input id="num1" type=text />
<input id="fun1" type=text />
<input id="num2" type=text />
<div>
<input type="button" id="btn" value="do"/>
</div>
<span id="result1"></span>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title</title>
<script type="text/javascript">
function $(id){return document.getElementById(id);}
window.onload = function(){
$("btn").onclick=function(){
var n1=$("num1").value;
var n2=$("num2").value;
if($("fun1").value=="+")
$("result1").innerText=Number(n1)+Number(n2);
if($("fun1").value=="-")
$("result1").innerText=Number(n1)-Number(n2);
if($("fun1").value=="*")
$("result1").innerText=Number(n1)*Number(n2);
if($("fun1").value=="/")
$("result1").innerText=Number(n1)/Number(n2);
};
};
</script>
</head>
<body>
<input id="num1" type=text />
<br>
<br>
<input id="fun1" type=text />
<br>
<br>
<input id="num2" type=text />
<br>
<br>
<div>
<input type="button" id="btn" value="do"/>
</div>
<br>
<br>
<span id="result1"></span>
</body>
</html>追问
就是在除数为0的时候那个要怎么编,弹出网页消息
追答<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title</title>
<script type="text/javascript">
function $(id){return document.getElementById(id);}
window.onload = function(){
$("btn").onclick=function(){
var n1=$("num1").value;
var n2=$("num2").value;
if($("fun1").value=="+")
$("result1").innerText=Number(n1)+Number(n2);
if($("fun1").value=="-")
$("result1").innerText=Number(n1)-Number(n2);
if($("fun1").value=="*")
$("result1").innerText=Number(n1)*Number(n2);
if($("fun1").value=="/")
if($("num2").value==0)
{
alert("除数不能为0!")//如果除数为零,添加报错!
}
else{
$("result1").innerText=Number(n1)/Number(n2);//除数不为零,正常运算~
}
};
};
</script>
</head>
<body>
<input id="num1" type=text />
<input id="fun1" type=text />
<input id="num2" type=text />
<div>
<input type="button" id="btn" value="do"/>
</div>
<span id="result1"></span>
</body>
</html>
温馨提示:答案为网友推荐,仅供参考