单击按钮时,我试图更改按钮文本和样式。代码在将“生成”更改为“重置”按钮时有效,但从“重置”到“生成”却不起作用。似乎代码只是在第一次点击时才起作用。我怎样才能解决这个问题?
// Change Generate Button to Reset#
let generateBtn = document.getElementById("generateBtn");
generateBtn.addEventListener("click", () => {
if ((generateBtn.innerHTML = "GENERATE")) {
generateBtn.innerHTML = "RESET";
generateBtn.classList.add("resetBtn-shown");
} else {
generateBtn.innerHTML = "GENERATE";
generateBtn.classList.remove("resetBtn-shown");
}
});
.generateBtn-shown {
background-color: red;
color: #eba341;
font-size: 1.5rem;
font-weight: 500;
border-radius: 0.5rem;
width: 10rem;
height: 3rem;
border: none;
}
.resetBtn-shown {
background-color: #21b884;
color: white;
}
<button id="generateBtn" class="generateBtn-shown">GENERATE</button>
慕丝7291255
有只小跳蛙
12345678_0001
相关分类