好友
阅读权限25
听众
最后登录1970-1-1
|
li0
发表于 2022-3-16 15:52
今天上课刚学的,来小水一下贴
软件:Visual Studio Code
先展示效果
效果图
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-UA-compatible" content="IE=edge">
<meta name="viewport" content="width=,initial-scale=1.0">
<title>简易计算器</title>
</head>
<script type="text/javascript">
function compute(obj){
var num1,num2,result;
num1 = parseFloat(document.myform.txtNum1.value);
num2 = parseFloat(document.myform.txtNum2.value);
switch(obj){
case"+":
result = num1 + num2;
break;
case"-":
result = num1 - num2;
break;
case"*":
result = num1 * num2;
break;
case"/":
if(num2!=0)
result = num1 / num2;
else
result="除数不也能为零,请重新输入!";
break;
}
document.myform.txtResult.value=result;
}
</script>
<body>
<form action=""method="post" name="myform" id="myform">
<table border="0">
<tr>
<td><img src=shop.gif width="54" height="54"></td>
<td colspan="3"><h3>购物简易计算器</h3></td>
</tr>
<tr>
<td>第一个数</td>
<td colspan="3"><input name="txtNum1" type="text" id="txtNum1" size="25"/></td>
</tr>
<tr>
<td>第二个数</td>
<td colspan="3"><input name="txtNum2" type="text" id="txtNum2" size="25"/></td>
</tr>
<tr>
<td><input name = "addButton2" type="button" id="addBtn" value="+" onclick="compute('+')"></td>
<td><input name = "subButton2" type="button" id="subBtn" value="-" onclick="compute('-')"></td>
<td><input name = "mulButton2" type="button" id="mulBtn" value="×" onclick="compute('*')"></td>
<td><input name = "divButton2" type="button" id="divBtn" value="÷" onclick="compute('/')"></td>
</tr>
<tr>
<td>计算结果</td>
<td colspan="3"><input name="txtResult" type="text" id="txtresult" size="25"/></td>
</tr>
</table>
</form>
</body>
</html>
跑步去了,告辞告辞
|
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|