gpt4 book ai didi

html - 在表中动态添加行/列时,如何保持容器大小?

转载 作者:行者123 更新时间:2023-11-27 23:41:56 28 4
gpt4 key购买 nike

var rows = 55;
var cols = 140;
var rowsInput = document.getElementById("rowsId");
var colsInput = document.getElementById("colsId");
//initialize 2dim arrays
var arr;// current generation array
var nextArr; // next generation array
function drawGrid() {
let grid = document.getElementById("container");
let table = document.createElement("table");
table.setAttribute("class", "center");

for (let i = 0; i < rows; i++) {
let tr = document.createElement("tr");
for (let j = 0; j < cols; j++) {
let cell = document.createElement("td");
cell.setAttribute("id", i + "_" + j);
cell.setAttribute("class", "dead");
tr.appendChild(cell);
cell.addEventListener("click", function () {
if (cell.classList.contains("live")) {
cell.setAttribute("class", "dead");
arr[i][j] = 0;
}
else
cell.setAttribute("class", "live");
arr[i][j] = 1;
});
}
table.appendChild(tr);
}
grid.appendChild(table);
}
function make2DArr() {
arr = new Array(rows);
nextArr = new Array(rows);
for (let i = 0; i < rows; i++) {
arr[i] = new Array(cols);
nextArr[i] = new Array(cols);
}
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
arr[i][j] = 0;
nextArr[i][j] = 0;
}
}
}
var generateBtn = document.getElementById("generateBtnId");

generateBtn.addEventListener("click", function () {
rows = rowsInput.value;
cols = colsInput.value;
remove();
make2DArr();
drawGrid();
});

function remove() {
var tb = document.querySelector("table");
tb.outerHTML = "";
}
function init() {
make2DArr();
drawGrid();
}
init();
body {
background-color: rgba(76, 77, 62, 0.514);
}
.center {

margin: auto;
width: 70rem;
height: 30rem;
padding: 0.5rem;
}
#container {
position: relative;
border:1px rgb(26, 51, 192) solid;
margin: 0;
overflow: auto;
display: flex;

}
table {
border:1px rgb(241, 241, 241) solid;
border-spacing: 0;
flex:1;
}
.live {
background-color:rgba(0, 0, 0, 0.685);
}
.dead {
background-color:rgba(228, 228, 241, 0.829);
}


td {
flex:1;

position: relative;
border:1px rgb(29, 182, 29) solid;
width: 0.5rem;
height: 0.5rem;
}

button {
background-color: #4CAF50; /* Green */
border: none;
color: white;
padding: 1rem 2rem;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 0.5rem 0.5rem;
transition-duration: 0.4s;
cursor: pointer;
}
button:hover {
background-color: rgba(144, 180, 145, 0.596);
color: rgb(54, 59, 54)
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel=stylesheet type="text/css" href="game.css">

</head>

<body>

<div class="center">
<div id="container">
</div>

<button id="startBtnId"><span>Start</span></button>
<button id="clearBtnId"><span>Clear</span></button>
<button id="randomBtnId"><span>Random</span></button>
<button id="generateBtnId"><span>Generate</span></button>
<input type="text" id="rowsId" value="rows">
<input type="text" id="colsId" value="cols">
<select id="sizeId">
<option value="Big">Big</option>
<option value="Medium">Medium</option>
<option value="Small">Small</option>
</select>

</div>

<script type="text/javascript" src="game.js"></script>
</body>
</html>
我想手动输入一些行和列,新表将包含在固定的容器大小中。例如,当我称量 20 行和 20 列时,容器的大小将与平衡 50 行和 50 列的容器的大小相同。输入行数和列数,然后点击生成

最佳答案

因为您已将容器指定为 flex,所以如果您添加尽可能多的列,它将适合容器,移除 flex 并手动提供宽度并检查。

关于html - 在表中动态添加行/列时,如何保持容器大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56991361/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com