如何在利用HTML,css和js的知识制作一个简单的网页计算器呢?
一个计算机中具备了:
计算机整体框:
/*设置div样式*/
#showdiv{
border: solid 1px;
border-radius: 5px;
width: 350px;
height: 400px;
text-align: center;
margin: auto;/*设置居中*/
margin-top: 50x;
background-color: rgb(214, 219, 190);
}
输入框:
/*设置输入框样式*/
input[type=text]{
margin-top: 20px;
width: 290px;
height: 40px;
font-size: 20px;
}
输入按钮:
/*设置按钮样式*/
input[type=button]{
width: 60px;
height: 60px;
margin-top: 20px;
margin-left: 5px;
margin-right: 5px;
font-size: 30px;
font-weight: bolder;
font-family: "楷书";
}
使用js代码对执行对应业务逻辑操作:
<!--声明js代码-->
<script>
function test(btn){
var number = btn.value;
switch (number) {
case "=":
document.getElementById("input").value= eval(document.getElementById("input").value);
break;
case "c":
document.getElementById("input").value="";
break;
default:
document.getElementById("input").value+=number;
break;
}
}
</script>
使用HTML对计算机进行排版布局:
<body>
<div id="showdiv">
<input type="text" id="input" readonly="readonly"><br>
<input type="button" value="1" onclick="test(this)">
<input type="button" value="2" onclick="test(this)">
<input type="button" value="3" onclick="test(this)">
<input type="button" value="4" onclick="test(this)"><br>
<input type="button" value="5" onclick="test(this)">
<input type="button" value="6" onclick="test(this)">
<input type="button" value="7" onclick="test(this)">
<input type="button" value="8" onclick="test(this)"><br>
<input type="button" value="9" onclick="test(this)">
<input type="button" value="+" onclick="test(this)">
<input type="button" value="-" onclick="test(this)">
<input type="button" value="*" onclick="test(this)"><br>
<input type="button" value="0" onclick="test(this)">
<input type="button" value="/" onclick="test(this)">
<input type="button" value="c" onclick="test(this)">
<input type="button" value="=" onclick="test(this)">
</div>
</body>
总体代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
#showdiv{
border: solid 1px;
border-radius: 5px;
width: 350px;
height: 400px;
text-align: center;
margin: auto;
margin-top: 50x;
background-color: rgb(214, 219, 190);
}
input[type=text]{
margin-top: 20px;
width: 290px;
height: 40px;
font-size: 20px;
}
input[type=button]{
width: 60px;
height: 60px;
margin-top: 20px;
margin-left: 5px;
margin-right: 5px;
font-size: 30px;
font-weight: bolder;
font-family: "楷书";
}
</style>
<script>
function test(btn){
var number = btn.value;
switch (number) {
case "=":
document.getElementById("input").value= eval(document.getElementById("input").value);
break;
case "c":
document.getElementById("input").value="";
break;
default:
document.getElementById("input").value+=number;
break;
}
}
</script>
<title>Document</title>
</head>
<body>
<div id="showdiv">
<input type="text" id="input" readonly="readonly"><br>
<input type="button" value="1" onclick="test(this)">
<input type="button" value="2" onclick="test(this)">
<input type="button" value="3" onclick="test(this)">
<input type="button" value="4" onclick="test(this)"><br>
<input type="button" value="5" onclick="test(this)">
<input type="button" value="6" onclick="test(this)">
<input type="button" value="7" onclick="test(this)">
<input type="button" value="8" onclick="test(this)"><br>
<input type="button" value="9" onclick="test(this)">
<input type="button" value="+" onclick="test(this)">
<input type="button" value="-" onclick="test(this)">
<input type="button" value="*" onclick="test(this)"><br>
<input type="button" value="0" onclick="test(this)">
<input type="button" value="/" onclick="test(this)">
<input type="button" value="c" onclick="test(this)">
<input type="button" value="=" onclick="test(this)">
</div>
</body>
</html>
实现效果:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210508194839934.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl81MTc0OTU1NA==,size_16,color_FFFFFF,t_70#pic_center)
你一定已经学会了前端网页计算机的制作了吧!