如何更改我在网格中单击的框旁边的框的颜色

我有一个 5x5 的网格,只要你点击它,每个盒子都会变成黑色或白色。这个网格模拟了熄灯游戏,到目前为止我只能改变我点击的盒子的颜色,但我还需要改变水平和垂直距离 1 长度的盒子的颜色。


我有一种感觉,我需要使用 javascript 将网格变成一个数组,但我不知道该怎么做。以下代码是我到目前为止所拥有的。


var grid = document.getElementsByClassName("box");


Array.from(grid).forEach(click => click.addEventListener("click", function changeColor() {

    if (click.style.background === 'black') {

        click.style.background = "white";

    } else {

        click.style.background = "black";

    }

}));

body {

  background-color: lightblue;

  margin: 40px;

}


.wrapper {

  display: grid;

  grid-template-columns: 100px 100px 100px 100px 100px;

  grid-gap: 3px;

}


.box {

  background-color: #fff;

  padding: 50px;

}

<!DOCTYPE html>

<html>

  <head>

    <title>Lights Out Game</title>

    <link href="lights_out.css" rel="stylesheet" type="text/css">

    <script type="module" src="Lightsout.js?v=5"></script>

  </head>

  

  <body>

    <h1>Lights Out</h1>

    <button type="button">Reset board</button>

    <div class="wrapper">

      <div class="box"></div>

      <div class="box"></div>

      <div class="box"></div>

      <div class="box"></div>

      <div class="box"></div>

      <div class="box"></div>

      <div class="box"></div>

      <div class="box"></div>

      <div class="box"></div>

      <div class="box"></div>

      <div class="box"></div>

      <div class="box"></div>

      <div class="box"></div>

      <div class="box"></div>

      <div class="box"></div>

      <div class="box"></div>

      <div class="box"></div>

      <div class="box"></div>

      <div class="box"></div>

      <div class="box"></div>

      <div class="box"></div>

      <div class="box"></div>

      <div class="box"></div>

      <div class="box"></div>

      <div class="box"></div>

    </div>


    <article> Click on each box until all the boxes are black.</article>

  </body>


德玛西亚99
浏览 131回答 1
1回答

心有法竹

由于点击动作会通过调用影响多个框changeColor,所以最好将其与事件监听器分开const changeColor = function(box){&nbsp; &nbsp; if (box.style.background === 'black') {&nbsp; &nbsp; &nbsp; &nbsp; box.style.background = "white";&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; box.style.background = "black";&nbsp; &nbsp; }}您仍然需要分配一个事件处理程序,我们将非常有创意地调用它 clickHandlerArray.from(grid).forEach(box => click.addEventListener("click", () => clickHandler(box)))区分每个盒子会很有用;可以通过id为它们中的每一个添加一个属性同时分配侦听器来完成,这样上面的行就变成了Array.from(grid).forEach(&nbsp; &nbsp; (box, index) => {&nbsp; &nbsp; &nbsp; &nbsp; box.addEventListener("click", () => clickHandler(box))&nbsp; &nbsp; &nbsp; &nbsp; box.id = index&nbsp; &nbsp; })最后,您需要确定单击某个框时需要更改哪些框。这可以在clickHandler函数中定义let boxes = Array.from(grid)const clickHandler= function(box){&nbsp; &nbsp; // get the box id, which will coincide with its position in the array&nbsp; &nbsp; index = parseInt(c.id)&nbsp; &nbsp; // determine the adjacent boxes' indexes&nbsp; &nbsp; up = index - 5&nbsp; &nbsp; down = index + 5&nbsp; &nbsp; left = index - 1&nbsp; &nbsp; right = index + 1&nbsp; &nbsp; // Make sure the index is not out of bounds and change color&nbsp; &nbsp; if(up > 0)&nbsp; &nbsp; &nbsp; &nbsp; changeColor(boxes[up])&nbsp; &nbsp; if(down < 25)&nbsp; &nbsp; &nbsp; &nbsp; changeColor(boxes[down])&nbsp; &nbsp; // Make sure the left and right indexes are in the same row&nbsp; &nbsp; if(Math.floor(index / 5) == Math.floor(left / 5))&nbsp; &nbsp; &nbsp; &nbsp; changeColor(boxes[left])&nbsp; &nbsp; if(Math.floor(index / 5) == Math.floor(right / 5))&nbsp; &nbsp; &nbsp; &nbsp; changeColor(boxes[right])&nbsp; &nbsp; // change the color of the box that was clicked&nbsp; &nbsp; changeColor(boxes[index])}放在一起应该是这样的:const grid = document.getElementsByClassName('box')const changeColor = function(box){&nbsp; &nbsp; if (box.style.background === 'black') {&nbsp; &nbsp; &nbsp; &nbsp; box.style.background = "white";&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; box.style.background = "black";&nbsp; &nbsp; }}Array.from(grid).forEach(&nbsp; &nbsp; (box, index) => {&nbsp; &nbsp; &nbsp; &nbsp; box.addEventListener("click", () => clickHandler(box))&nbsp; &nbsp; &nbsp; &nbsp; box.id = index&nbsp; &nbsp; })let boxes = Array.from(grid)const clickHandler= function(box){&nbsp; &nbsp; // get the box id, which will coincide with its position in the array&nbsp; &nbsp; index = parseInt(box.id)&nbsp; &nbsp; // determine the adjacent boxes' indexes&nbsp; &nbsp; up = index - 5&nbsp; &nbsp; down = index + 5&nbsp; &nbsp; left = index - 1&nbsp; &nbsp; right = index + 1&nbsp; &nbsp; // Make sure the index is not out of bounds and change color&nbsp; &nbsp; if(up > 0)&nbsp; &nbsp; &nbsp; &nbsp; changeColor(boxes[up])&nbsp; &nbsp; if(down < 25)&nbsp; &nbsp; &nbsp; &nbsp; changeColor(boxes[down])&nbsp; &nbsp; // Make sure the left and right indexes are in the same row&nbsp; &nbsp; if(Math.floor(index / 5) == Math.floor(left / 5))&nbsp; &nbsp; &nbsp; &nbsp; changeColor(boxes[left])&nbsp; &nbsp; if(Math.floor(index / 5) == Math.floor(right / 5))&nbsp; &nbsp; &nbsp; &nbsp; changeColor(boxes[right])&nbsp; &nbsp; // change the color of the box that was clicked&nbsp; &nbsp; changeColor(boxes[index])}body {&nbsp; background-color: lightblue;&nbsp; margin: 40px;}.wrapper {&nbsp; display: grid;&nbsp; grid-template-columns: 100px 100px 100px 100px 100px;&nbsp; grid-gap: 3px;}.box {&nbsp; background-color: #fff;&nbsp; padding: 50px;}<!DOCTYPE html><html>&nbsp; <head>&nbsp; &nbsp; <title>Lights Out Game</title>&nbsp; &nbsp; <link href="lights_out.css" rel="stylesheet" type="text/css">&nbsp; &nbsp; <script type="module" src="Lightsout.js?v=5"></script>&nbsp; </head>&nbsp;&nbsp;&nbsp; <body>&nbsp; &nbsp; <h1>Lights Out</h1>&nbsp; &nbsp; <button type="button">Reset board</button>&nbsp; &nbsp; <div class="wrapper">&nbsp; &nbsp; &nbsp; <div class="box"></div>&nbsp; &nbsp; &nbsp; <div class="box"></div>&nbsp; &nbsp; &nbsp; <div class="box"></div>&nbsp; &nbsp; &nbsp; <div class="box"></div>&nbsp; &nbsp; &nbsp; <div class="box"></div>&nbsp; &nbsp; &nbsp; <div class="box"></div>&nbsp; &nbsp; &nbsp; <div class="box"></div>&nbsp; &nbsp; &nbsp; <div class="box"></div>&nbsp; &nbsp; &nbsp; <div class="box"></div>&nbsp; &nbsp; &nbsp; <div class="box"></div>&nbsp; &nbsp; &nbsp; <div class="box"></div>&nbsp; &nbsp; &nbsp; <div class="box"></div>&nbsp; &nbsp; &nbsp; <div class="box"></div>&nbsp; &nbsp; &nbsp; <div class="box"></div>&nbsp; &nbsp; &nbsp; <div class="box"></div>&nbsp; &nbsp; &nbsp; <div class="box"></div>&nbsp; &nbsp; &nbsp; <div class="box"></div>&nbsp; &nbsp; &nbsp; <div class="box"></div>&nbsp; &nbsp; &nbsp; <div class="box"></div>&nbsp; &nbsp; &nbsp; <div class="box"></div>&nbsp; &nbsp; &nbsp; <div class="box"></div>&nbsp; &nbsp; &nbsp; <div class="box"></div>&nbsp; &nbsp; &nbsp; <div class="box"></div>&nbsp; &nbsp; &nbsp; <div class="box"></div>&nbsp; &nbsp; &nbsp; <div class="box"></div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <article> Click on each box until all the boxes are black.</article>&nbsp; </body>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript