简体   繁体   English

style.backgroundColor 不起作用?

[英]style.backgroundColor doesn't work?

Tried to change the color of a cell by clicking on it.试图通过单击来更改单元格的颜色。

The cells are normaly grey and by the first click they should turn red.单元格通常是灰色的,第一次点击它们应该变成红色。 When I click on a red cell it should turn grey again.当我单击红色单元格时,它应该再次变为灰色。

 function changeColor(cell) { var red = '#FE2E2E'; var grey = '#E6E6E6'; if (cell.style.backgroundColor == grey) { cell.style.backgroundColor = red; } else { if (cell.style.backgroundColor == red) { cell.style.backgroundColor = grey; } }; };
 #table tr td { width: 20px; height: 50px; cursor: pointer; background-color: #E6E6E6; border: 1px solid black; }
 <table class="table table-bordered" id="table"> <tbody> <tr> <td onclick="changeColor(this)"></td> <td onclick="changeColor(this)"></td> <td onclick="changeColor(this)"></td> <td onclick="changeColor(this)"></td> <td onclick="changeColor(this)"></td> </tr> </tbody> </table>

I also tried with .style.bgColor , rgb and if (cell.style.backgroundColor === but this didn't work either. The value of the cells background color is either by .backgroundColor : '' or by .bgColor : undefined .我也尝试过.style.bgColorrgbif (cell.style.backgroundColor ===但这也不起作用。单元格背景颜色的值是.backgroundColor : ''.bgColor : undefined .

The value you get back from style.backgroundColor isn't likely to be in the same format you set it in;您从style.backgroundColor返回的值可能与您设置的格式不同; it's in whatever format the browser wants to make it.它是浏览器想要制作的任何格式。

One minimal change approach is to store a flag on the element (see comments):一种最小的更改方法是在元素上存储一个标志(见评论):

 function changeColor(cell) { var red = '#FE2E2E'; var grey = '#E6E6E6'; // Get a flag; will be falsy if not there at all var flag = cell.getAttribute("data-grey"); if (!flag) { // Make it grey cell.setAttribute("data-grey", "true"); cell.style.backgroundColor = red; } else { // Not grey, make it red cell.setAttribute("data-grey", ""); // blank is falsy cell.style.backgroundColor = grey; } }
 #table tr td { width: 20px; height: 50px; cursor: pointer; background-color: #E6E6E6; border: 1px solid black; }
 <table class="table table-bordered" id="table"> <tbody> <tr> <td onclick="changeColor(this)"></td> <td onclick="changeColor(this)"></td> <td onclick="changeColor(this)"></td> <td onclick="changeColor(this)"></td> <td onclick="changeColor(this)"></td> </tr> </tbody> </table>

...but the right thing to do is add/remove classes and use CSS to style accordingly (see comments): ...但正确的做法是添加/删除类并使用 CSS 进行相应的样式设置(请参阅评论):

 // See https://developer.mozilla.org/en-US/docs/Web/API/Element/classList for classList info function changeColor(cell) { // adds or removes the active class cell.classList.toggle("active"); }
 #table tr td { width: 20px; height: 50px; cursor: pointer; background-color: #E6E6E6; border: 1px solid black; } /* A class we can toggle to override the color above */ #table tr td.active { background-color: #fe2e2e; }
 <table class="table table-bordered" id="table"> <tbody> <tr> <td onclick="changeColor(this)"></td> <td onclick="changeColor(this)"></td> <td onclick="changeColor(this)"></td> <td onclick="changeColor(this)"></td> <td onclick="changeColor(this)"></td> </tr> </tbody> </table>

Your code isn't working because the style property does not have a backgroundColor set when your code first runs: style represents an element's inline style attribute, and your elements do not have one to start.您的代码无法正常工作,因为在您的代码第一次运行时, style属性没有设置backgroundColorstyle表示元素的内联样式属性,而您的元素没有要开始的属性。 When you check to see if the element's background is red or gray , it's neither since it has no inline style ( style.backgroundColor is actually the empty string).当您检查元素的背景是red还是gray ,两者都不是,因为它没有内联样式( style.backgroundColor实际上是空字符串)。

You have a few options:您有几个选择:

  • Use getComputedStyle to see what the element's background-color really is, regardless if it's set inline or not.使用getComputedStyle查看元素的background-color到底是什么,无论它是否设置为内联。
  • Provide a default case that will set your element's background-color regardless of whether or not it's already been set.提供一个默认情况,它会设置元素的background-color而不管它是否已经设置。 (If it's red, switch it to gray; otherwise, set it to red.) (如果是红色,则将其切换为灰色;否则,将其设置为红色。)

Either would work and do what you need it to;要么工作,要么做你需要做的事情; it depends on how flexible you need to be in your solution, and I'll leave that up to you.这取决于您在解决方案中需要有多灵活,我将把它留给您。

So this is how your if/else should be written, you should not have a check inside of your else.所以这就是你的 if/else 应该如何写,你不应该在你的 else 里面检查。 Now the console.log will show why your code fails.现在 console.log 将显示您的代码失败的原因。 The backgroundColor check will return rgb in most browsers and not the hex. backgroundColor 检查将在大多数浏览器中返回 rgb 而不是十六进制。

...This does not work on purpose... ......这不是故意的......

 function changeColor(cell) { var red = '#FE2E2E'; var grey = '#E6E6E6'; console.log(cell.style.backgroundColor); //rgb(230, 230, 230) if (cell.style.backgroundColor == grey) { cell.style.backgroundColor = red; } else { cell.style.backgroundColor = grey; } };
 #table tr td { width: 20px; height: 50px; cursor: pointer; background-color: #E6E6E6; border: 1px solid black; }
 <table class="table table-bordered" id="table"> <tbody> <tr> <td onclick="changeColor(this)"></td> <td onclick="changeColor(this)"></td> <td onclick="changeColor(this)"></td> <td onclick="changeColor(this)"></td> <td onclick="changeColor(this)"></td> </tr> </tbody> </table>

So what can you do?那你能做什么? Use a function like RGB to Hex and Hex to RGB to convert the hex/rgb, Or you can use rgb instead and make it work in most browsers rgb(230, 230, 230) and rgb(254, 46, 46 OR the easy thing with a lot less code is to use a class and do not worry about colors at all.使用像RGB to Hex 和 Hex to RGB这样的函数来转换 hex/rgb,或者你可以使用 rgb 代替并使其在大多数浏览器中工作 rgb(230, 230, 230) 和 rgb(254, 46, 46 OR the easy代码少得多的事情是使用一个类,根本不用担心颜色。

 function changeColor(cell) { console.log(cell); cell.classList.toggle("selected"); };
 #table tr td { width: 20px; height: 50px; cursor: pointer; background-color: #E6E6E6; border: 1px solid black; } #table tr td.selected { background-color: #FE2E2E; }
 <table class="table table-bordered" id="table"> <tbody> <tr> <td onclick="changeColor(this)"></td> <td onclick="changeColor(this)"></td> <td onclick="changeColor(this)"></td> <td onclick="changeColor(this)"></td> <td onclick="changeColor(this)"></td> </tr> </tbody> </table>

now 2020, and i find the really solution:现在是 2020 年,我找到了真正的解决方案:

function changeColor(cell) {
  var red = "rgba(255, 4, 10, 1)"; //rgba or rgb, so if you really want to use this, you need use regexp. 
  var grey = "rgba(230, 230, 230, 1)"; //pls change your css to this too.


  var style = cell.computedStyleMap().get('background-color').toString();

  if (style == grey) {
    cell.style.backgroundColor = red;
  } else  if (style == red) {
    cell.style.backgroundColor = grey; 
  };

for chrome this is ok, but, maybe other browser with other color format, you need test it.对于 chrome,这是可以的,但是,也许其他具有其他颜色格式的浏览器,您需要对其进行测试。

You need quotation marks here.这里需要引号。

function changeColor(cell) {

  if (cell.style.backgroundColor == "grey") {
    cell.style.backgroundColor = "red";
  } else if (cell.style.backgroundColor == "red") {
    cell.style.backgroundColor = "grey";
  };
};

(I also took the liberty to replace the if in the else block and replaced it with a else if) (我也冒昧地替换了 else 块中的 if 并用 else if 替换了它)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM