我正在做一个初学者的小项目。if语句不起作用
if语句
const value = document.getElementById("value")
const btnDecrease = document.querySelector(".decrease")
const btnReset = document.querySelector(".reset")
const btnIncrease = document.querySelector(".increase")
let count = 0
btnDecrease.addEventListener("click", function() {
count--
value.textContent = count
})
btnIncrease.addEventListener("click", function() {
count++
value.textContent = count
})
btnReset.addEventListener("click", function() {
count = 0
value.textContent = count
})
if (count > 0) {
value.style.color = "Green";
} else if (count < 0) {
value.style.color = "red";
} else {
value.style.color = "#222"
}
如果计数为负值,我应该将其变为红色,如果为正值,则变为绿色
1条答案
按热度按时间hsvhsicv1#
当前
if
语句只运行一次,然后才输入任何值。将其提取到函数中,并在每次计数更改后调用它。