简体   繁体   中英

Looping divs in a div works, but adjusting position does not

I am making a webpage. I have one main DIV (relative) with lots of DIV's(absolutes) inside. I want to change the position and size of DIV's inside the main DIV with javascript.

This works. Got it from stackoverflow. Loops trough al the divs and gives me the information i want to change. Size and position.

function getDivs(){ 
var loopdiv = document.getElementById('loopdiv');
var divs=loopdiv.getElementsByTagName('div') 

for (var i=0;i<divs.length;i++){ 

alert("left:"+divs[i].style.left
 +"   top:"+divs[i].style.top
 +"   height:"+divs[i].style.height
 +"   width:"+divs[i].style.width
 ); 

}  } 

So i thought let's change it up. Let's add little code so all my div's are at 0,0 position.

function getDivs(){ 
var loopdiv = document.getElementById('loopdiv');
var divs=loopdiv.getElementsByTagName('div') 

for (var i=0;i<divs.length;i++){ 

divs[i].style.left=0;
divs[i].style.top=0;



} 

Should be pretty crowded with div's right now on 0,0.... but nothing. Every div just retains their initial position from the HTML.

Does anyone know what's wrong?

You have a typo!

Change

divs[i].sytle.top=0;

to

divs[i].style.top=0;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM