样式:
<!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">
<title>Document</title>
<style>
html,body {
margin: 0px;
padding: 0px;
background-color: rgb(175, 119, 119);
}
p{
font-size: 32px ;
font-family:serif;
text-align: center;
margin-top: 30px;
font-weight:bold;
}
.container {
width: 430px;
height: 500px;
margin-left: 560px;
margin-top: 30px;
background-color: #5a5959;
border: 1px solid black;
}
table {
width: 390px;
height: 380px;
margin-left: 22px;
border: 1px solid rgb(0, 0, 0);
background-color: #666666;
}
input {
margin-top: 20px;
margin-left: 24px;
width: 378px;
height: 38px;
opacity: 0.6;
font-size: 20px;
color: black;
border-radius: 5px;
margin-right: 5px;
background-color: whitesmoke;
}
button{
width: 48px;
height: 36px;
margin-top: 16px;
text-align: center;
margin-left: 23px;
/* margin-bottom: 8px; */
border-radius: 6px;
font:14px black bold;
border: 1px solid black;
background-color: rgb(211, 207, 207);
box-shadow: 2px 2px rgb(0, 0, 0);
cursor: pointer;
}
button:hover{
background-color: rgb(31, 28, 28);
color: whitesmoke;
}
</style>
</head>
框架
<body>
<hr>
<!-- js练习 实现网页版计算器 -->
<p>简 易 计 算 器</p>
<div class="container">
<table>
<input type="text" name="look" id="look"><br><br>
<tr>
<td><button value="CE" onclick="cle()">CE</button></td>
<td><button value="+" onclick="compute(this)">+</button></td>
<td><button value="-" onclick="compute(this)">-</button></td>
<td><button value="DEL" onclick="del()">DEL</button></td>
</tr>
<tr>
<td><button value="7" onclick="compute(this)">7</button></td>
<td><button value="8" onclick="compute(this)">8</button></td>
<td><button value="9" onclick="compute(this)">9</button></td>
<td><button value="*" onclick="compute(this)">*</button></td>
</tr>
<tr>
<td><button value="4" onclick="compute(this)">4</button></td>
<td><button value="5" onclick="compute(this)">5</button></td>
<td><button value="6" onclick="compute(this)">6</button></td>
<td><button value="/" onclick="compute(this)">/</button></td>
</tr>
<tr>
<td><button value="1" onclick="compute(this)">1</button></td>
<td><button value="2" onclick="compute(this)">2</button></td>
<td><button value="3" onclick="compute(this)">3</button></td>
<td><button value="." onclick="compute(this)">.</button></td>
</tr>
<tr>
<td><button value="(" onclick="compute(this)">(</button></td>
<td><button value="0" onclick="compute(this)">0</button></td>
<td><button value=")" onclick="compute(this)">)</button></td>
<td><button value="=" onclick="result()">=</button></td>
</tr>
</table>
</div>
script
<script>
function compute(show){
document.getElementById("look").value+=show.value;
}
function cle(){
document.getElementById("look").value=null;
}
function del(){
var num = document.getElementById("look");
num.value = num.value.substring(0,num.value.length-1);
}
function result(){
str = document.getElementById("look");
numresult = eval(str.value);
str.value = numresult
}
</script>
</body>
</html>