Create a simple calculator with buttons and input fields
HTML
<div class="first">計數器:
<button class="sub" onclick="Subtraction()">-</button>
<button class="add" onclick="Addition()">+</button>
</div>
<div class="second">簡單計算機
<input type="text" class="num1"/>
<input type="text" class="num2"/>
<button onclick="caall()">加/減/乘/除</button>
</div>
CSS
* {
box-sizing: border-box;
}
.first {
width: 300px;
}
.sub {
width: 50px;
}
.add {
width: 50px;
}
.second input {
width: 60px;
}
JavaScript
let total1 = 0;
function Subtraction() {
total1 --;
alert(total1);
}
function Addition() {
total1 ++ ;
alert(total1);
}
let caall = () => {
let num1 = parseInt(document.querySelector('.num1').value);
let num2 = parseInt(document.querySelector('.num2').value);
alert(num1 + num2);
alert(num1 - num2);
alert(num1 * num2);
alert(num1 / num2);
}