首页 > 代码库 > js-计算器
js-计算器
<div class="main"><h1>HTML5-计算器</h1>
<input id="num1" class="num" type="number"/>
<span id="operator">+</span>
<input id="num2" class="num" type="number"/>
<a>=</a>
<input id="num3" class="num" type="number" disabled="disabled"/>
<input id="add" class="operator" type="button" value="http://www.mamicode.com/+">
<input id="sub" class="operator" type="button" value="http://www.mamicode.com/-">
<input id="mul" class="operator" type="button" value="http://www.mamicode.com/*">
<input id="div" class="operator" type="button" value="http://www.mamicode.com/÷">
<input id="qc" class="operator" type="button" value="http://www.mamicode.com/清除">
</div>
<script>
var num1=document.getElementById("num1");
var num2=document.getElementById("num2");
var num3=document.getElementById("num3");
var num;
add.onclick=function(){
operator.innerHTML="+";
num3.value=http://www.mamicode.com/parseInt(num1.value)+parseInt(num2.value);
}
sub.onclick=function(){
operator.innerHTML="-";
num3.value=http://www.mamicode.com/parseInt(num1.value)-parseInt(num2.value);
}
mul.onclick=function(){
operator.innerHTML="*";
num3.value=http://www.mamicode.com/parseInt(num1.value)*parseInt(num2.value);
}
div.onclick=function(){
operator.innerHTML="/";
num3.value=http://www.mamicode.com/parseInt(num1.value)/parseInt(num2.value)
}
qc.onclick=function(){
num1.value=http://www.mamicode.com/‘‘;
num2.value=http://www.mamicode.com/‘‘;
num3.value=http://www.mamicode.com/‘‘;
}
</script>
js-计算器