我試圖運行一個腳本來在正方形內繪製正方形圖案。所以我做到了。marginTop在javascript中不工作
var bodyNode = document.getElementsByTagName("body")[0];
var width = 600, height = 600;
var top = 10, left = 10;
while(width > 30)
{
var divNode = document.createElement("div");
divNode.style.position = "absolute";
divNode.style.marginLeft = left;
divNode.style.marginTop = top;
divNode.style.width = width;
divNode.style.height = height;
divNode.style.background = randomColor();
bodyNode.appendChild(divNode);
width -= 10;
height -= 10;
top += 10;
left += 10;
}
function randomColor()
{
var color = "#";
for(var i =0; i < 6; i++)
{
var number = Math.floor(Math.random() * 10);
color += number;
}
return color;
}
但它被添加到div在運行時的屬性是:
element.style {
position: absolute;
margin-left: 260px;
width: 350px;
height: 350px;
background: rgb(88, 88, 5);
}
我想實現this效果,但我得到一個不同的模式如下圖所示的形象。
我錯過了什麼?
P.S:我不使用jQuery。
請發佈您的jsfiddle。 – Alex