標籤:cas 初學者 option height 計算機 click charset style count()
####閑來無事,寫點小東西給初學者,下面是一個簡易的計算機,我們來看具體的代碼:
<!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>
js之簡易計算機