css 按钮更改最新的div项,而不是正确的

0kjbasz6  于 2023-10-21  发布在  其他
关注(0)|答案(2)|浏览(163)

我创建了一个div表,其中有一个按钮,该按钮创建了一个框,应该发生的是,您可以单击任何{x}个框,它将其着色为黑色。然而,它不是将该框着色为黑色(或白色),而是将最新的框着色为黑色(或白色)。我该如何更改此设置?
超文本标记语言:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>Rule 110</title>
  7. <link href="style.css" rel="stylesheet" type="text/css" />
  8. </head>
  9. <body>
  10. <script src="script.js"></script>
  11. <center><h1>Rule 110</h1></center>
  12. <div class="grid-container">
  13. <main id="content"></main>
  14. </div>
  15. <button onclick=add() class="button">+</button>
  16. </body>
  17. </html>

JavaScript语言:

  1. const first = document.getElementById("first")
  2. const second = document.getElementById("second")
  3. const third = document.getElementById("third")
  4. const fourth = document.getElementById("fourth")
  5. const fifth = document.getElementById("fifth")
  6. const sixth = document.getElementById("sixth")
  7. const seventh = document.getElementById("seventh")
  8. const eigth = document.getElementById("eigth")
  9. const ninth = document.getElementById("ninth")
  10. var extra = 1;
  11. var thingrowx=0;
  12. var thingrowy = 1;
  13. function changeColor(test) {
  14. if (document.getElementById(test).className === "grid-item-white"){
  15. document.getElementById(test).className="grid-item-black";}
  16. else {
  17. document.getElementById(test).className="grid-item-white";
  18. }
  19. }
  20. function createTable() {
  21. rn = window.prompt("Input number of rows", 1);
  22. cn = window.prompt("Input number of columns",1);
  23. for(var r=0;r<parseInt(rn,10);r++)
  24. {
  25. var x=document.getElementById('myTable').insertRow(r);
  26. for(var c=0;c<parseInt(cn,10);c++)
  27. {
  28. var y= x.insertCell(c);
  29. y.innerHTML="Row-"+r+" Column-"+c;
  30. }
  31. }
  32. }
  33. function repeat(func, times) {
  34. func;
  35. times && --times && repeat(func, times);
  36. }
  37. function test() {
  38. document.querySelector('#content').insertAdjacentHTML(
  39. 'afterbegin',
  40. `<div class="grid-item-white" id="combined" onclick=changeColor('combined')>
  41. </div>`
  42. )
  43. }
  44. function add() {
  45. extra += 1;
  46. thingrowx += 1;
  47. var combined = "(" + thingrowx + "," + thingrowy + ")"
  48. alert(combined)
  49. repeat(test(), thingrowy)
  50. }

CSS:

  1. .button {
  2. border: none;
  3. background-color: black;
  4. padding: 16px 32px;
  5. text-align: center;
  6. text-decoration: none;
  7. display: inline-block;
  8. font-size: 16px;
  9. margin: 4px 2px;
  10. transition-duration: 0.4s;
  11. cursor: pointer;
  12. color: white;
  13. }
  14. .button1 {
  15. background-color: white;
  16. color: black;
  17. border: 2px solid #4CAF50;
  18. }
  19. .button1:hover {
  20. background-color: #4CAF50;
  21. color: white;
  22. }
  23. .button2 {
  24. background-color: white;
  25. color: black;
  26. border: 2px solid #008CBA;
  27. }
  28. .button2:hover {
  29. background-color: #008CBA;
  30. color: white;
  31. }
  32. .grid-container {
  33. display: grid;
  34. grid-template-columns: 50px 50px 50px 50px 0px 50px 50px 50px 0px 50px 50px 50px 50px 50px 50px 50px;
  35. padding: 10px;
  36. }
  37. .grid-item-white {
  38. background-color: white;
  39. border: 1px solid rgba(0, 0, 0, 0.8);
  40. padding: 20px;
  41. font-size: 30px;
  42. text-align: center;
  43. }
  44. .grid-item-black {
  45. background-color: black;
  46. border: 1px solid rgba(0, 0, 0, 0.8);
  47. padding: 20px;
  48. font-size: 30px;
  49. text-align: center;
  50. color: white
  51. }
  52. body {background-color: #cdf1eb ;}
tyky79it

tyky79it1#

我认为问题出在test函数上。

  1. function test() {
  2. document.querySelector('#content').insertAdjacentHTML(
  3. 'afterbegin',
  4. `<div class="grid-item-white" id="combined" onclick=changeColor('combined')>
  5. </div>`
  6. )
  7. }

对于每个新添加的元素,将id设置为"combined"。但是id对于整个html文档中的每个元素都必须是唯一的。你可能打算做以下事情。

  1. function test(combined) {
  2. // use the combined index that is passed from the add function
  3. document.querySelector('#content').insertAdjacentHTML(
  4. 'afterbegin',
  5. // -------------------------------vvvvvvvvvvv-- use template literal
  6. `<div class="grid-item-white" id="${combined}" onclick=changeColor('${combined}')>
  7. </div>`
  8. )
  9. }
  10. function add() {
  11. extra += 1;
  12. thingrowx += 1;
  13. var combined = "(" + thingrowx + "," + thingrowy + ")"
  14. alert(combined)
  15. repeat(test(combined), thingrowy) // pass the combined index to the test method
  16. }

您的代码非常混乱,命名不当,还有许多未使用的变量和函数,因此我尝试了一点重构!

  1. const contentElement = document.getElementById("content");
  2. contentElement.addEventListener("click", (event) => {
  3. // here event.target represents the element that has been clicked on
  4. changeColor(event.target);
  5. });
  6. let rowX = 0;
  7. let rowY = 1;
  8. function changeColor(element) {
  9. if (element.className === "grid-item-white")
  10. element.className = "grid-item-black";
  11. else element.className = "grid-item-white";
  12. }
  13. function add() {
  14. rowX += 1;
  15. const combined = "(" + rowX + "," + rowY + ")";
  16. // alert(combined);
  17. document
  18. .querySelector("#content")
  19. .insertAdjacentHTML(
  20. "afterbegin",
  21. `<div class="grid-item-white" data-id="${combined}"></div>`
  22. );
  23. }
  1. .button {
  2. border: none;
  3. background-color: black;
  4. padding: 16px 32px;
  5. text-align: center;
  6. text-decoration: none;
  7. display: inline-block;
  8. font-size: 16px;
  9. margin: 4px 2px;
  10. transition-duration: 0.4s;
  11. cursor: pointer;
  12. color: white;
  13. }
  14. .button1 {
  15. background-color: white;
  16. color: black;
  17. border: 2px solid #4caf50;
  18. }
  19. .button1:hover {
  20. background-color: #4caf50;
  21. color: white;
  22. }
  23. .button2 {
  24. background-color: white;
  25. color: black;
  26. border: 2px solid #008cba;
  27. }
  28. .button2:hover {
  29. background-color: #008cba;
  30. color: white;
  31. }
  32. .grid-container {
  33. display: grid;
  34. grid-template-columns: 50px 50px 50px 50px 0px 50px 50px 50px 0px 50px 50px 50px 50px 50px 50px 50px;
  35. padding: 10px;
  36. }
  37. .grid-item-white {
  38. background-color: white;
  39. border: 1px solid rgba(0, 0, 0, 0.8);
  40. padding: 20px;
  41. font-size: 30px;
  42. text-align: center;
  43. }
  44. .grid-item-black {
  45. background-color: black;
  46. border: 1px solid rgba(0, 0, 0, 0.8);
  47. padding: 20px;
  48. font-size: 30px;
  49. text-align: center;
  50. color: white;
  51. }
  52. body {
  53. background-color: #cdf1eb;
  54. }
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width" />
  6. <title>Rule 110</title>
  7. <link href="style.css" rel="stylesheet" type="text/css" />
  8. </head>
  9. <body>
  10. <center>
  11. <h1>Rule 110</h1>
  12. </center>
  13. <button onclick="add()" class="button">+</button>
  14. <div class="grid-container">
  15. <main id="content"></main>
  16. </div>
  17. <script src="script.js"></script>
  18. </body>
  19. </html>

在这里,我使用了事件委托来监听任何网格项上的单击事件。因此,如果我们监听#content元素上的点击事件并使用event.target,那么我们就会得到被点击的网格项。然后我们可以将该元素传递给changeColor方法。

展开查看全部
wlwcrazw

wlwcrazw2#

我认为这是因为你所有的div都有相同的id(“combined”),所以浏览器会得到DOM中最上面的一个。如果你给予他们每个人一个唯一的id,这个问题就可以解决。这对我很有效:

  1. `<div class="grid-item-white" id="combined${extra}" onclick=changeColor('combined${extra}')`

相关问题