【问题标题】:Adding items in array and retrieving them fails to work在数组中添加项目并检索它们无法正常工作
【发布时间】:2016-10-16 17:54:29
【问题描述】:

我想用 6 个带有 HTML 和 CSS 的盒子做一个猜谜游戏,然后给每个盒子一个唯一的颜色。

我生成 RGB 中的颜色,但它没有生成数组,数组只有 1,2,3,4,5,6。

var colors = [];
var squares = document.querySelectorAll(".square");
function generateColors (num){
  var r = Math.floor(Math.random()* 256);
  var g = Math.floor(Math.random()* 256);
  var b = Math.floor(Math.random()* 256);
  var rgb = "rgb(" + r + ", " + g + ", " + g + ")";

  var i = 0;
  for(i; i <= num-1; i++){
    colors[i] = colors.push(rgb);
  }
  for(i; i <= num-1; i++){
    squares[i].style.background = colors[i];
  }
  console.log(rgb);
}

generateColors(6);

【问题讨论】:

    标签: javascript html css arrays


    【解决方案1】:

    您只定义每个变量(r、g 和 b)一次,因此它们不会是 6 种不同的颜色。您还可以将颜色作为字符串生成。这对你来说非常好阅读,但 css 不会得到这个。改用 rgb() 函数,如下所示:squares[i].style.background.rgb(r,g,b);

    【讨论】:

      【解决方案2】:

      您的代码中几乎没有错误。

      首先,不要使用colors[i] = colors.push(rgb),而是使用colors.push(rgb)

      其次,您必须在 for 循环的每次迭代中丢失新颜色,这就是为什么您必须将 Math.random 操作放入循环中的原因。否则,您每次都会丢失相同的 rgb。

      var colors = [];
      var squares = document.querySelectorAll(".square");
      
      function generateColors(num) {
      
        for (var i = 0; i < num; i++) {
          var r = Math.floor(Math.random() * 256);
          var g = Math.floor(Math.random() * 256);
          var b = Math.floor(Math.random() * 256);
          var rgb = "rgb(" + r + ", " + g + ", " + b + ")";
          colors.push(rgb);
        }
      
        for (var i = 0; i < num; i++) {
          squares[i].style.background = colors[i];
        }
      
      }

      【讨论】:

      • 蓝色部分还是g应该是var rgb = "rgb(" + r + ", " + g + ", " + b + ")";
      • @AndreCanilho 没错。谢谢!
      • 设置背景颜色的代码永远不会运行,因为你之前有return colors;
      • @MikeMcCaughan 温馨提示。谢谢!
      【解决方案3】:

      rgb 值应该在循环内以加载颜色。

          var colors = [];
          var squares = document.querySelectorAll(".square");
      
          function generateColors(num) {
            var i = 0;
            for (i; i <= num - 1; i++) {
              var r = Math.floor(Math.random() * 256);
              var g = Math.floor(Math.random() * 256);
              var b = Math.floor(Math.random() * 256);
              var rgb = "rgb(" + r + ", " + g + ", " + b + ")";
              colors.push(rgb);
            }
            for (i; i <= num - 1; i++) {
              squares[i].style.background = colors[i];
            }
            console.log(colors);
          }
          generateColors(6);

      【讨论】:

        【解决方案4】:

        一些事情。尽可能优化您的 cicles。 您生成的值在 cicle 之外,因此它们只被调用一次。 您在 rgb 中打印“蓝色”部分时有一个小错误。

        var colors;
        var squares;
        function generateColors(num){
          squares = document.querySelectorAll('.square');
          colors = [];
          var r,g,b, rgb;
          for(var i = 0; i < num; i++){
            r = Math.floor(Math.random()* 256);
            g = Math.floor(Math.random()* 256);
            b = Math.floor(Math.random()* 256);
            rgb = "rgb(" + r + ", " + g + ", " + b + ")";
            colors.push(rgb);
            squares[i].style.background = rgb;
          }
        }
        body{
          display:flex;  
        }
        .square{
          width:20px;
          height:20px;
          background:#aaa;
          border: 1px solid black;
          padding:5px;
        }
        <div class="square">
        A
        </div>
        <div class="square">
        B
        </div>
        <div class="square">
        C
        </div>
        <div class="square">
        D
        </div>
        <div class="square">
        E
        </div>
        <div class="square">
        F
        </div>
        <button onclick="generateColors(6)">Generate</button>

        【讨论】:

        • 我将不胜感激对 downvote 的评论。这是什么原因?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-09-09
        • 1970-01-01
        • 1970-01-01
        • 2015-08-09
        • 2020-08-21
        • 2014-07-17
        • 1970-01-01
        相关资源
        最近更新 更多