这篇文章主要为大家详细介绍了javascript制作计算器的方法,文章中的示例代码非常详细,若有兴趣可以学习参考,闲言少叙直接上代码:
<!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>
以上就是javascript制作计算器的详细内容,代码示例简单明了,如果在日常工作遇到此问题。通过这篇文章,希望你能有所收获,更多详情敬请关注天达云行业资讯频道!