use*_*681 1 html javascript css html-table
这是我的代码如下.我试图扭转已经发出的点击.这是用td标签发生的.我在这里和其他论坛上结合了几个问题的失败实验.
function changeColor(elem) {
if (elem.style.backgroundColor = "#5AD9C1" || "transparent") {
elem.style.backgroundColor = "#66FF66";
} else if (element.style.backgroundColor = "#66FF66") {
elem.style.backgroundColor = "#5AD9C1";
}
};
Run Code Online (Sandbox Code Playgroud)
第一:
// With '=' you do assign a value to backgroundColor
if (elem.style.backgroundColor = "#5AD9C1" ...)
// Use '==' to check, if a equals b
if(elem.style.backgroundColor == "#5AD9C1" ...)
Run Code Online (Sandbox Code Playgroud)
第二:
你不能像这样链接if语句:
if(myVar == 'A' || 'B' || 'C')
Run Code Online (Sandbox Code Playgroud)
这与询问是否('B')始终为真是一样的
你必须这样做:
if(myVar == 'A' || myVar == 'B' || myVAR == 'C')
Run Code Online (Sandbox Code Playgroud)
有关if语句和运算符的详细信息,请参阅https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else
正确的解决方案是:
function changeColor(elem) {
if (elem.style.backgroundColor == "#5AD9C1" || elem.style.backgroundColor == "transparent") {
elem.style.backgroundColor = "#66FF66";
} else if (element.style.backgroundColor == "#66FF66") {
elem.style.backgroundColor = "#5AD9C1";
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
正如评论中所提到的,这不起作用的主要原因是因为style.backgroundColor将颜色作为RGB值返回
我找到了将rgb转换为hex的解决方案.