【发布时间】:2021-10-03 10:09:46
【问题描述】:
使用地图功能并显示的按钮不止一个。想要更改我单击的按钮的背景颜色。而其他人则希望它保持原样。当我再次单击另一个按钮时,仅更改该按钮的 BG 颜色。 这是我已经拥有的:
const handleClick = (option, index) => {
const nextQuestion = currentQuestion + 1;
if (data[currentQuestion].answer === option) {
// setChangeColor(isCorrect);
isCorrect = "bg-success text-white";
setTimeout(() => {
setCurrentQuestion(nextQuestion);
setMoneyLadder(moneyLadder - 1);
setChangeColor("");
}, 2000);
} else {
// setChangeColor(isIncorrect);
isIncorrect = "bg-danger";
setTimeout(() => {
window.location.reload();
setCurrentQuestion(0);
setMoneyLadder(15);
setChangeColor("");
}, 2000);
}
};
<div className="answers">
{data[currentQuestion].options.map(
(option, index) => (
<button
className={
// data[currentQuestion].answer ===
// option
// ? "text-success"
// : ""
}
key={index}
onClick={() =>
handleClick(option, index)
}
dangerouslySetInnerHTML={{
__html: `${index + 1}) ${option}`,
}}
></button>
)
)}
</div>
有什么想法吗?
【问题讨论】:
标签: javascript reactjs