【问题标题】:change background color with JavaScript用 JavaScript 改变背景颜色
【发布时间】:2016-07-22 04:37:41
【问题描述】:

我有一个主 div,其中包含 n * n 个 div,它们有黑色背景或白色背景。

我希望他们通过点击主 div 来更改他们的 BGcolor。

这是我的代码(显然不起作用)。

    function invert(){


   var divs = document.getsElementsByTagName("div");

   for(i=0; i<divs.length; i++){
          if(divs[i].style.backgroundColor=="black")
          {
            divs[i].style.backgroundColor="white";
          }
          else if (divs[i].style.backgroundColor=="white") 
          {
            divs[i].style.backgroundColor="black";
          }



   }

}

【问题讨论】:

标签: javascript css colors background onclick


【解决方案1】:

如果您没有明确设置背景颜色,它可能仍然是 ""(至少我在 Firefox 中看到的)所以您的 if 条件都不匹配。

如果您检测到当前颜色未设置,您也可以切换为黑色:

var color = divs[i].style.backgroundColor;  
if (color === "black")
    divs[i].style.backgroundColor = "white";
else if (!color || color === "white") 
    divs[i].style.backgroundColor = "black";

【讨论】:

  • 我确实明确设置了背景
【解决方案2】:

为所有DIVs 设置默认的白色背景色。

为其中一些添加black 类。

使用classList.toggle 交替颜色:

document.body.onclick= function() {
  var divs= document.querySelectorAll('div');
  for(var i = 0 ; i < divs.length ; i++) {
    divs[i].classList.toggle('black');
  }
}
body, html {
  height: 100%;
  background: lightyellow;
}

div {
  width: 50px;
  height: 50px;
  border: 1px solid #333;
  display: inline-block;
  background: white;
}

.black {
  background: black;
}
<div class="black"></div>
<div></div>
<div class="black"></div>
<div></div>
<div class="black"></div>
<div class="black"></div>
<div></div>
<div></div>

【讨论】:

  • 这更好,原因有很多。例如,假设您决定从“黑色/白色”更改为“深灰色/浅灰色”,您可以在 CSS 中这样做,而无需更改一行 JavaScript。此外,许多浏览器在将white 用作颜色样式时会将rgb(255,255,255)“翻译”为rgb(255,255,255),从而导致比较失败。类更容易管理。
【解决方案3】:

实现此目的的两种方法:

1-style.backgroundColor(如果你让背景颜色信息内联):

function turnthelights() {
  
var x = document.querySelectorAll(".inner");
var i;
for (i = 0; i < x.length; i++) {
  
if (x[i].style.backgroundColor === "black"){
    x[i].style.backgroundColor = "white";
} else {
    x[i].style.backgroundColor = "black";      
}
}  
}
body {
  background-color: greenyellow;  
}

.inner {
  width: 80px;
  height: 80px;
  display: inline-block;  
  outline: 2px solid gold;
}
<div id=container onclick="turnthelights()"> 
  
<div class="inner" style="background-color: black"></div>  
<div class="inner" style="background-color: white"></div>  
<div class="inner" style="background-color: black"></div>  
<div class="inner" style="background-color: white"></div>  
<div class="inner" style="background-color: black"></div>
<div class="inner" style="background-color: white"></div>
  
</div>

2-getComputedStyle(element).backgroundColor(bg 颜色信息任何地方):

function turnthelights() {
  
var x = document.querySelectorAll(".inner");
var i;
for (i = 0; i < x.length; i++) {

  var k = x[i];
  var z = getComputedStyle(k).backgroundColor;
  
if (z == "rgb(0, 0, 0)"){
    x[i].style.backgroundColor = "rgb(255, 255, 255)";
} else {
    x[i].style.backgroundColor = "rgb(0, 0, 0)";      
}
}  
}
body {
  background-color: hotpink;  
}

.inner {
  width: 80px;
  height: 80px;
  display: inline-block;  
  outline: 2px solid gold;
}

.black {
  background-color: rgb(0, 0, 0);  
}

.white {
  background-color: rgb(255, 255, 255);  
}
<div id=container onclick="turnthelights()"> 
  
<div class="inner black"></div>  
<div class="inner white"></div>  
<div class="inner black"></div>  
<div class="inner white"></div>  
<div class="inner black"></div>
<div class="inner white"></div>
  
</div>

【讨论】:

    【解决方案4】:

    现在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;
        }
    }
    

    chrome 是可以的,但是,可能其他颜色格式的浏览器,你需要测试一下。

    【讨论】:

      猜你喜欢
      • 2013-04-28
      • 2012-04-02
      • 2015-06-23
      • 1970-01-01
      • 2016-08-21
      • 2013-07-20
      • 2015-05-23
      相关资源
      最近更新 更多