【发布时间】:2019-10-17 21:37:27
【问题描述】:
在这个页面上:
http://javascript.drawyourpets.com/
每次用户单击按钮时,我都会尝试在样式之间来回切换。
我正在使用来自this solution 的代码的开关盒:
function changeColor() {
let theToggle = document.getElementById("change-color");
theToggle.toggleStatus = "on";
switch (theToggle.toggleStatus) {
case "on":
theToggle.toggleStatus = "off";
theToggle.style.color = "purple";
theToggle.style.backgroundColor = "yellow";
break;
case "off":
theToggle.toggleStatus = "on";
theToggle.style.color = "yellow";
theToggle.style.backgroundColor = "purple";
break;
}
}
#change-color {
width: 100px;
height: 100px;
color: yellow;
background-color: purple
}
<div class="column">
<button id="change-color" onclick="changeColor()">Change Color</button>
</div>
这会在第一次改变背景颜色,但它只工作一次 - 它应该在每次点击时改变。
我对switch-cases不是很熟悉,可能有什么问题?
【问题讨论】:
标签: javascript html css switch-statement