js之简易计算器
####闲来无事,写点小东西给初学者,下面是一个简易的计算器,我们来看具体的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>简易计算器</title>
<style>
div{
height: 100px;
margin: 200px 350px;
}
</style>
<script type="text/javascript">
function getId(idname){
return document.getElementById(idname)
}
function count(){
switch(getId("select").value){
case "+":
getId("txt3").value=parseInt(getId("txt1").value)+parseInt(getId("txt2").value)
break;
case "-":
getId("txt3").value=getId("txt1").value-getId("txt2").value
break;
case "*":
getId("txt3").value=getId("txt1").value*getId("txt2").value
break;
case "/":
getId("txt3").value=getId("txt1").value/getId("txt2").value
break;
}
}
</script>
</head>
<body>
<div>
<input type="text" id="txt1">
<select id="select">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" id="txt2">
<button onclick="count()">=</button>
<input type="text" id="txt3" onclick="count()">
</div>
</body>
</html>