你好我所有的我都是html和js的初學者,我正在嘗試創建一個包含矩形集合的網頁,其中當創建一個新的矩形時,將放置在前一個矩形的旁邊。 我創建了一個div
元素,並嘗試添加新創建的div
(矩形形狀,背景色根據條件而異),但我無法獲得所需的結果。在html中使用js創建動態矩形集合
<html>
<head>
<title>parkIn</title>
<meta charset="utf-8" />
</head>
<style>
.ParkSlots {
border: solid 1px;
width: 60%;
height: 400px;
top: 50%;
left: 50%;
position: fixed;
transform: translate(-50%, -50%);
display: inline;
}
.row:before,
.row:after {
content: "";
display: table;
clear: both;
}
.col-1 {
width: 15%;
margin-left: 10px;
height: 350px;
padding: 2px;
}
</style>
<body onload="viewCreate()">
<div class="ParkSlots">
<div class="row" id="content">
</div>
</div>
</body>
<script language="javascript">
function viewCreate() {
for (int i = 0; i < 5; i++) {
if (i % 2 == 0) {
createGreenBox();
} else {
createRedBox();
}
}
}
function createRedBox() {
var = document.createElement('div');
div.className = 'col-1';
div.style.backgroundColor = 'red';
document.getElementById('content').appendChild(div);
}
function createGreenBox() {
var = document.createElement('div');
div.className = 'col-1';
div.style.backgroundColor = 'lightgreen';
document.getElementById('content').appendChild(div);
}
</script>
</html>
我想要的輸出,看起來是這樣的:
您能告訴我們您目前看到的內容,包括任何控制檯錯誤嗎? – Will
首先嚐試用'for(var i = 0; i <5; i ++)替換'for(int i = 0; i <5; i ++)'''int'不以那種方式在js中使用。同時給你的變量div創建名稱'div'。 – Shilly
感謝您的快速回復..它幫助我很多傢伙.. – user6654733