简体   繁体   English

创建一个10x10的单元格字段

[英]Create a 10x10 field of cells

I want to create a field of cells. 我想创建一个单元格字段。 The field has a size of 10x10. 该字段的大小为10x10。 When reaching a maximum count of cells in a row, it should start a new row. 当达到一行的最大单元数时,它应该开始新的一行。

Currently all my cell divs are placed below. 目前,我所有的单元格div都位于下面。

 function initGame() { var mapSize = 10; // create a field of 10x10 var cellsPerRow = 10; // 10 cells per row for (var x = 0; x < mapSize; x++) { for (var y = 0; y < mapSize; y++) { createCell(x, y); // create a cell on index x (horizontal) and y (vertical) } } } function createCell(x, y) { // store this cell position to a data class var cellDiv = $("<div></div>"); // create the cell div cellDiv.addClass("cell"); // add some css $(document.body).append(cellDiv); // add the cell div to the parent } 
 .cell{ height: 50px; width: 50px; border-style: solid; border-width: 1px; border-color: black; background: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <body onLoad="initGame()"> </body> 

use display:inline-block; 使用display:inline-block; for .cells and to stop 10 each row add a <br> tag after 10 divs in row. 对于.cell并要在每行停止10个位置,请在每行10个div后添加<br>标记。

 function initGame() { var mapSize = 10; // create a field of 10x10 var cellsPerRow = 10; // 10 cells per row for (var x = 0; x < mapSize; x++) { for (var y = 0; y < mapSize; y++) { createCell(x, y); } $(document.body).append("<br>"); } } function createCell(x, y) { // store this cell position to a data class var cellDiv = $("<div></div>"); // create the cell div cellDiv.addClass("cell"); // add some css $(document.body).append(cellDiv); // add the cell div to the parent } 
 .cell { height: 50px; width: 50px; border-style: solid; border-width: 1px; border-color: black; background: red; display: inline-block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <body onLoad="initGame()"> </body> 

Create wrapper for each 10 cell. 每10个单元格创建一个包装器。

 function initGame() { var mapSize = 10; // create a field of 10x10 var cellsPerRow = 10; // 10 cells per row for (var x = 0; x < mapSize; x++) { $(document.body).append("<div>"); for (var y = 0; y < mapSize; y++) { createCell(x, y); // create a cell on index x (horizontal) and y (vertical) } $(document.body).append("</div>"); } } function createCell(x, y) { // store this cell position to a data class var cellDiv = $("<div></div>"); // create the cell div cellDiv.addClass("cell"); // add some css $(document.body).append(cellDiv); // add the cell div to the parent } 
 .cell{ height: 50px; width: 50px; border-style: solid; border-width: 1px; border-color: black; background: red; display: inline-block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <body onLoad="initGame()"> </body> 

You can use absolute positioning and set for each of them css property. 您可以使用绝对定位并为每个css属性设置。 Here is the fiddle 这是小提琴

 function initGame() { var mapSize = 10; // create a field of 10x10 var cellsPerRow = 10; // 10 cells per row for (var x = 0; x < mapSize; x++) { for (var y = 0; y < mapSize; y++) { createCell(x, y); // create a cell on index x (horizontal) and y (vertical) } } } function createCell(x, y) { // store this cell position to a data class var cellDiv = $("<div></div>"); // create the cell div cellDiv.addClass("cell"); // add some css cellDiv.css({ left: Math.floor(x*50), top: Math.floor(y*50) }); $(document.body).append(cellDiv); // add the cell div to the parent } initGame() 
 .cell{ height: 50px; width: 50px; border-style: solid; border-width: 1px; border-color: black; background: red; position: absolute; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 

You can create a row div that contains multiple cell div 你可以创建一个行div包含多个单元div

 function initGame() { const rows = 10; const columns = 10; for(let r=0;r<rows;r++){ let row = createRow(r); for(let c=0;c<columns;c++){ createCell(row, r, c); } } } function createRow(rowNumber){ let row = document.createElement('DIV'); row.className += ' row' $(document.body).append(row); return row; } function createCell(domRow, rowNumber, columnNumber){ let column = document.createElement('DIV'); column.className += ' cell'; $(domRow).append(column); } 
 .cell{ height: 50px; width: 50px; border: 1px solid grey; background: tomato; display: inline-block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <body onLoad="initGame()"> </body> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建一个 10x10 字段的字段 - creating a field of 10x10 field 制作具有 10x10 单元格的表格,每个单元格的内部编号从 1 到 100 - Make table which have cells 10x10 and every cells have inside number from 1 to 100 JS:如何创建一个10x10的填充矩形网格? - JS: How do I create a 10x10 grid of filled rectangles? 如何制作一个由“#”字符组成的10x10矩形? - How to make a 10x10 rectangle of “#” characters? 为什么我的程序创建10x9网格而不是10x10网格? - Why is my program creating a 10x9 grid instead of a 10x10 grid? 使用easyl.js创建10x10图像网格,图像交叉随机溶解 - Creating a 10x10 image grid using easel.js where images cross dissolve randomly HTML / CSS:具有相等边距的10x10 div框,但是由于某种原因,高度长于宽度 - HTML/CSS: 10x10 div boxes with equal margin, but height is longer than width for some reason 需要有关HTML / JS方法的帮助,从此处开始构建10x10乘法表,此表应稍后在动态 - Need help over the methodology of HTML/JS, where to start building a 10x10 multiplication table that should be later on dynamic Minimax Alpha Beta修剪算法花费太多时间来解决井字游戏(10x10电路板) - Minimax Alpha Beta Pruning Algorithm takes too much time to solve Tic Tac Toe (10x10 board) 使用javascript创建10 * 10表格,并用黑色到白色填充单元格 - Create 10*10 table using javascript and populate cells with colors from black to white
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM