首页 > 代码库 > js之简易计算器
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=http://www.mamicode.com/parseInt(getId("txt1").value)+parseInt(getId("txt2").value)
break;
case "-":
getId("txt3").value=http://www.mamicode.com/getId("txt1").value-getId("txt2").value
break;
case "*":
getId("txt3").value=http://www.mamicode.com/getId("txt1").value*getId("txt2").value
break;
case "/":
getId("txt3").value=http://www.mamicode.com/getId("txt1").value/getId("txt2").value
break;
}
}
</script>
</head>
<body>
<div>
<input type="text" id="txt1">
<select id="select">
<option value="http://www.mamicode.com/+">+</option>
<option value="http://www.mamicode.com/-">-</option>
<option value="http://www.mamicode.com/*">*</option>
<option value="http://www.mamicode.com/">/</option>
</select>
<input type="text" id="txt2">
<button onclick="count()">=</button>
<input type="text" id="txt3" onclick="count()">
</div>
</body>
</html>
js之简易计算器