<!doctype?html>
<html>
<head>
<meta?charset="UTF-8">
<title>Document</title>
<style?type="text/css">
div
{
width:?50px;
height:?50px;
background:?red;
position:?absolute;
top:0;
left:?0;
font-size:?30px;
text-align:?center;
line-height:?50px;
color:?#fff;
}
</style>
<script>
window.onload?=?function??()?{
var?aDiv?=?document.getElementsByTagName('div');
var?str?='';
//批量創(chuàng)建div
for?(var?i?=?0;?i?<?31;?i++)?{
str?+='<div>'+i+'</div>';
alert(str);
};
//alert(str);
//坐標設置
for?(var?i?=?0;?i?<?aDiv.length;?i++)?{
//間隔10px同行排列
aDiv[i].style.left?=?10+i*60?+?'px';
//逢10個換行
for?(j=1;i>=10*j;j++)?{
aDiv[i].style.top?=?j*60+'px';
aDiv[i].style.left?=?10+(i-j*10)*60?+?'px';
}?;
//階梯狀排列
//aDiv[i].style.left?=?10+i*50?+?'px';
//aDiv[i].style.top?=?10+i*50?+?'px';
//“V”字型排列
//aDiv[i].style.left?=?10+i*50?+?'px';
//if?(i<aDiv.length/2)?{
//????aDiv[i].style.top?=?10+i*50?+?'px';
//}?else{
//?aDiv[i].style.top?=?10+(aDiv.length-1-i)*50?+?'px';
//};
};
document.body.innerHTML?=?str;
}
</script>
</head>
<body>
</body>
</html>為什么我只要定義了str ,就只能出最后一個div方塊;如果直接用document.body.innerHTML 就能出現(xiàn)正確的效果。還有給div數(shù)組的長度定義 len,也會出問題。是哪里有沖突嗎?視頻里也沒有做其他設置呀。
js中定義賦值的問題
qq_電波_03505597
2016-07-07 15:16:46