javascript - 我有一个按钮,我需要在第一次单击时打开颜色并在第二次单击时更改回原始颜色
问题描述
我可以在第一次单击时使按钮更改颜色,但在第二次单击时不能。不知道如何执行。控制台日志按预期设置为 true。
var clicked = true,
button = document.getElementById("buttonProps");
button.addEventListener("click", function () {
if (clicked) {
this.style.backgroundColor = "skyblue";
} else {
this.style.backgroundColor = "lightgray";
}
});
console.log(clicked)
解决方案
您需要切换单击的变量
var clicked = true
button = document.getElementById("buttonProps");
button.addEventListener("click", function() {
if (clicked) {
this.style.backgroundColor = "skyblue";
} else {
this.style.backgroundColor = "lightgray";
}
clicked = !clicked
});
console.log(clicked)
推荐阅读
- javascript - 如何将phaser3游戏嵌入html
- antrl4 - ANTLR - 我可以让解析器上下文对象类型定义吗?
- java - 如何去除Android Material Design中TextInputLayout下的下划线?
- java - 在java中使用服务器时间更新firestore文档
- arrays - 英语句子hypenation
- javascript - guildMemberAdd 和 guildMemberRemove 嵌入不再发送(Discord.js)
- android - 如何真正重置editText以显示修改后的提示
- tensorflow-federated - TFF:如何拆分每个客户端的数据
- python - 如何让tensorflow同时使用cpu和gpu
- java - 如何为相似文本(语义相似性)生成相同的哈希?