gpt4 book ai didi

javascript - 为什么点击时我的颜色没有变化?

转载 作者:行者123 更新时间:2023-11-28 19:27:24 24 4
gpt4 key购买 nike

所以我有一个非常简单的代码,当我运行它时没有给出任何错误,但问题是,它没有做它应该做的......

当我点击中间的 div 时,它应该改变颜色...

我错过了什么?似乎找不到任何错字...

var panel = document.getElementById('color_panel');
document.getElementById('color_panel').addEventListener("click", function() {
var mySwitch = document.getElementById('color_panel').style.background;
if (mySwitch == "#76FF03") {
document.getElementById('color_panel').style.background = "#039BE5";
} else {
document.getElementById('color_panel').style.background = "#76FF03";
}
});
<body style="background: #37474F; margin: 0;">
<div id="color_panel" style="background: #76FF03; margin:
auto; font-family: Century Gothic; font-size: 40px; color: #37474F;
width: 200px; height: 200px; padding: 20px; margin-top: 50%;
transform: translateY(-50%); text-align: center;">
Click me!<br>or Double Click?</div>
<button style="font-size: 10px;
width: 100px; height: 20px; margin-left: 50%;
transform: translateX(-50%); border: solid 2px;
border-color: #FBC02D; border-radius: 5px;
background: #E3F2FD;" id="button" onclick="transition()">
add transition!</button>

最佳答案

您需要进行一些基本调试。添加一个 console.log 语句来检查您在 if 语句中输入的值并查看它的实际值。

浏览器正在对其进行规范化,因此 mySwitch == "#76FF03" 永远不会为真。

var panel = document.getElementById('color_panel');
document.getElementById('color_panel').addEventListener("click", function() {
var mySwitch = document.getElementById('color_panel').style.background;
console.log(mySwitch);
if (mySwitch == "#76FF03") {
document.getElementById('color_panel').style.background = "#039BE5";
} else {
document.getElementById('color_panel').style.background = "#76FF03";
}
});
<body style="background: #37474F; margin: 0;">
<div id="color_panel" style="background: #76FF03; margin:
auto; font-family: Century Gothic; font-size: 40px; color: #37474F;
width: 200px; height: 200px; padding: 20px; margin-top: 50%;
transform: translateY(-50%); text-align: center;">
Click me!<br>or Double Click?</div>
<button style="font-size: 10px;
width: 100px; height: 20px; margin-left: 50%;
transform: translateX(-50%); border: solid 2px;
border-color: #FBC02D; border-radius: 5px;
background: #E3F2FD;" id="button" onclick="transition()">
add transition!</button>

这种类型的问题通常可以通过将所有样式放在单独的样式表中然后调整元素所属的类来更好地处理。

var panel = document.getElementById('color_panel');

panel.addEventListener("click", function() {
panel.classList.toggle("other");
});
body {
background: #37474F;
margin: 0;
}

#color_panel {
background: #76FF03;
margin: auto;
font-family: Century Gothic;
font-size: 40px;
color: #37474F;
width: 200px;
height: 200px;
padding: 20px;
margin-top: 50%;
transform: translateY(-50%);
text-align: center;
}

#color_panel.other {
background: #039BE5;
}

#button {
font-size: 10px;
width: 100px;
height: 20px;
margin-left: 50%;
transform: translateX(-50%);
border: solid 2px;
border-color: #FBC02D;
border-radius: 5px;
background: #E3F2FD;
}
<div id="color_panel">
Click me!<br>or Double Click?
</div>

<button style="" id="button" onclick="transition()">
add transition!
</button>

关于javascript - 为什么点击时我的颜色没有变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55878195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com