[英]How do I make a grid using divs?
谁能告诉我我的代码有什么问题? 我正在尝试使用 div 创建网格。 这是我到目前为止所拥有的,我不确定它为什么不创建网格。 我已经用谷歌搜索了很多次,我不知道为什么它不起作用。 编码新手,任何帮助将不胜感激。
const container = document.querySelector('.container') function makeGrid(); for (i = 0, i < 16, i++); { const row = document.createElement("div") container.document.body.appendChild(row); row.textContent = i; for (j = 0, j < 16, j++); { const col = document.getElementById('div'); container.document.body.appendChild(col); col.textContent = j; } row.appendChild(col) container.appendChild(row) } makeGrid();
html, body { height: 100%; width: 100%; } body { display: grid; grid-column-gap: 0px; grid-row-gap: 0px; grid-template-columns: repeat(3, auto); }.container { width: 20px; height: 20px; text-align: center; border: 1px solid blue; background-color: pink; display: inline-block; float: left; }
<html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>Etch-a-Sketch TOP Project</title> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <script src="script.js"></script> <div class="container"></div> </body> </html>
这是一个简单的工作解决方案,您可以用作参考
const container = document.querySelector(".container");
function makeGrid() {
for (i = 0; i < 16; i++) {
for (j = 0; j < 16; j++) {
const row = document.createElement("div");
container.appendChild(row);
row.textContent = `${i},${j}`
}
}
}
makeGrid();
html,
body {
height: 100%;
width: 100%;
}
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
text-align: center;
}
.container div{
width: 30px;
height: 30px;
border: 1px solid blue;
background-color: pink;
}
您的代码中存在一些关于语法和逻辑的问题。
for (i = 0, i < 16, i++)
应该
for (i = 0; i < 16; i++)
for (i = 0; i < 16; i++); {
// repetitive code
}
由于分号,将无法正常工作。 要使其正常工作,您需要删除; 像这样
for (i = 0; i < 16; i++) {
// repetitive code
}
const col = document.getElementById('div');
div 不是 ID,它是 HTML 标记。 所以你可以将上面的语句替换为
const col = document.getElementsByTagsName('div');
你写的 javascript function 是错的。 我修改了你的几行代码。 试试这个。!
const container = document.querySelector(".container"); function makeGrid() { for (i = 0; i < 16; i++) { const row = document.createElement("div"); row.className = "row"; container.appendChild(row); row.textContent = i; for (j = 0; j < 16; j++) { const col = document.createElement("div"); col.className = "col"; row.appendChild(col); col.textContent = j; } } } makeGrid();
html, body { height: 100%; width: 100%; } body { display: grid; grid-column-gap: 0px; grid-row-gap: 0px; grid-template-columns: repeat(3, auto); }.container div { width: 20px; height: 20px; text-align: center; border: 1px solid blue; background-color: pink; display: inline-block; float: left; }
<html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>Etch-a-Sketch TOP Project</title> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <script src="script.js"></script> <div class="container"></div> </body> </html>
当您定义 function 时;
不需要。 所以应该是这样function makeGrid(){... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.