如何移除函数内创建的HTML类?Etch A Sketch网格调整求助
Etch A Sketch网格调整问题
我正在进行The Odin Project的Etch A Sketch项目,通过DOM操作创建了网格。希望通过移除旧网格并让用户输入新尺寸来调整网格大小,但无法访问createGrid()函数内创建的节点。请问能否在保留现有循环的前提下,删除与网格相关的已创建元素?
我曾尝试在resizeGrid()中调用verticalBoxes.remove(),但因该变量不在全局作用域而无效;还尝试移除并重建container元素,却因变量重复声明出现问题。
代码示例
HTML代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Etch A Sketch</title> <link rel="stylesheet" href="styles.css"> <script src= "scripts.js" defer></script> </head> <body> <h1>Etch A Sketch</h1> <div id= "container"></div> <div id= "grid-size"> <button type="confirm" id= "resize-button">Resize Grid</button> </div> </body> </html>
CSS代码
#container { margin: auto; max-width: 500px; max-height: 500px; } h1 { text-align:center; } .row { display:flex; height: auto; } .column { flex: 1; width: 100%; aspect-ratio: 1; border: 1px solid black; } .resize-button { display: inline-block; width:50px; height:50px; }
JavaScript代码
let container = document.querySelector("#container"); const button = document.querySelector("#resize-button") function createGrid(num) { for (let i = 0; i < num; i++) { let horizontalBoxes = document.createElement("div"); container.appendChild(horizontalBoxes); horizontalBoxes.classList.add("row"); for (let y = 0; y < num; y++) { let verticalBoxes = document.createElement("div"); horizontalBoxes.appendChild(verticalBoxes); verticalBoxes.classList.add("column"); verticalBoxes.addEventListener('mouseover', colorChange); } } } function colorChange () { this.style.backgroundColor = "black" } createGrid(16); function resizeGrid(newSize) { newSize = prompt("What size would you like the grid to be? (1-100)"); createGrid(newSize); } button.addEventListener('click', resizeGrid);
解决方案
不用尝试访问createGrid()内部的局部变量,直接清空container容器内的所有子元素即可,同时补充输入验证确保尺寸合法:
1. 核心修改:清空旧网格
在调用createGrid()之前,用以下任意一种方式清空container:
- 方法一(推荐):设置innerHTML为空
container.innerHTML = '';
- 方法二:循环移除子节点
while (container.firstChild) { container.removeChild(container.firstChild); }
2. 补充输入验证
避免用户输入无效值(非数字、超出1-100范围),在resizeGrid()里添加验证逻辑:
修改后的完整JavaScript代码
let container = document.querySelector("#container"); const button = document.querySelector("#resize-button") function createGrid(num) { for (let i = 0; i < num; i++) { let horizontalBoxes = document.createElement("div"); container.appendChild(horizontalBoxes); horizontalBoxes.classList.add("row"); for (let y = 0; y < num; y++) { let verticalBoxes = document.createElement("div"); horizontalBoxes.appendChild(verticalBoxes); verticalBoxes.classList.add("column"); verticalBoxes.addEventListener('mouseover', colorChange); } } } function colorChange () { this.style.backgroundColor = "black"; } createGrid(16); function resizeGrid() { let newSize = prompt("What size would you like the grid to be? (1-100)"); // 输入验证:确保是1-100之间的有效数字 newSize = parseInt(newSize); if (isNaN(newSize) || newSize < 1 || newSize > 100) { alert("请输入1到100之间的有效数字!"); return; } // 清空旧网格 container.innerHTML = ''; // 创建新网格 createGrid(newSize); } button.addEventListener('click', resizeGrid);
说明
- 清空container后,旧的
.row和.column元素都会被移除,不需要单独访问函数内的局部变量 - 输入验证避免了非法尺寸导致的布局问题
- 完全保留了你原本的
createGrid()循环逻辑,没有改动核心创建代码
内容的提问来源于stack exchange,提问作者kalper
相关产品推荐
相关产品推荐

