<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>JS计算器</title>
<style>
body,p,input{
margin: 0;
padding: 0;
}
.wp{
width: 500px;
height: 500px;
border:10px solid black;
margin: 0 auto;
margin-top: 100px;
}
#text{
width: 480px;
height: 90px;
border-bottom: 10px solid black;
font-size: 50px;
line-height: 100px;
text-align: left;
padding-left: 20px;
background: #ffffff;
}
input{
width: 125px;
height: 100px;
background: #c0c0c0;
float: left;
font-size: 40px;
line-height: 100px;
text-align: center;
}
</style>
<script>
window.onload = function () {
var oText = document.getElementById('text');
var aBtn = document.getElementsByTagName('input');
var aText = oText.value
for(i=0;i<aBtn.length;i++){
aBtn[i].onclick = conunt;
};
function conunt(value){
if(this.value == 'C'){
oText.value ='';
}else if(this.value == '='){
oText.value = eval(oText.value)
}else if(this.value == 'x'){
oText.value -= oText.value.substring( oText.value.length);
}
else{
oText.value += this.value;
};
};
};
</script>
</head> <body> <div class="wp"> <input type="text" id="text"> <input type="button" id="btn1" value="C" > <input type="button" id="btn2" value="/"> <input type="button" id="btn3" value="*"> <input type="button" id="btn4" value="-"> <input type="button" id="btn5" value="1"> <input type="button" id="btn6" value="2"> <input type="button" id="btn7" value="3"> <input type="button" id="btn8" value="+"> <input type="button" id="btn9" value="4"> <input type="button" id="btn10" value="5"> <input type="button" id="btn11" value="6"> <input type="button" id="btn12" value="."> <input type="button" id="btn13" value="7"> <input type="button" id="btn14" value="8"> <input type="button" id="btn15" value="9"> <input type="button" id="btn16" value="="> </div> </body> </html>