Ich möchte ein Feld von Zellen erstellen. Das Feld hat eine Größe von 10x10. Wenn eine maximale Anzahl von Zellen in einer Zeile erreicht wird, sollte eine neue Zeile beginnen.Erstellen Sie ein 10x10 Feld von Zellen
Momentan sind alle meine Zellen divs unterhalb platziert.
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>
Sorry, was ist Ihre Frage? Bitte beschreiben Sie, was Sie wollen und wie der aktuelle Code davon abweicht. – Richard
Die Zellen sollten die Spalten auf der rechten Seite füllen. Es sollte 10 Zellen pro Zeile geben – peterHasemann
Warum nicht einfach eine Tabelle erstellen? (Und machbar in CSS, wenn jede Zeile in einem separaten Element mit 'display: table/table-row/table-cel;'.) Liegt – Richard