我有我的屏幕(1,2)上的两个按钮。我希望能够存储被点击在变量哪个按钮,并添加5(如果按钮1)或减去5(如果按钮2)的总成绩。如何存储已被点击了哪个按钮?
var total = 0
$("#btn1").on("click", function() {
total = total + 5;
$("#txt1").val(total);
});
$("#btn2").on("click", function() {
total = total - 5;
$("#txt1").val(total);
})
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#banner-message {
background: #fff;
border-radius: 4px;
padding: 20px;
font-size: 25px;
text-align: center;
transition: all 0.2s;
margin: 0 auto;
width: 300px;
}
button {
background: #0084ff;
border: none;
border-radius: 5px;
padding: 8px 14px;
font-size: 15px;
color: #fff;
}
#banner-message.alt {
background: #0084ff;
color: #fff;
margin-top: 40px;
width: 200px;
}
#banner-message.alt button {
background: #fff;
color: #000;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="btn1">+5</button>
<button id="btn2">-5</button>
<input type="text" value="0" id="txt1" />