HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>布局转换</title> </head> <body> <ul id="ul1"> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> </ul> </body> </html>
CSS:
* {margin:0; padding:0;} #ul1 {width:366px; margin:0 auto; position:relative;} #ul1 li {list-style:none; width:100px; height:100px; background:#CCC; border:1px solid black; float:left; margin:10px;}
JS:
window.onload=function () { var oUl=document.getElementById('ul1'); var aLi=oUl.getElementsByTagName('li'); var i=0; for(i=0;i<aLi.length;i++) { //aLi[i].innerHTML='left:'+aLi[i].offsetLeft+', top:'+aLi[i].offsetTop; aLi[i].style.left=aLi[i].offsetLeft+'px'; aLi[i].style.top=aLi[i].offsetTop+'px'; } for(i=0;i<aLi.length;i++) { aLi[i].style.position='absolute'; aLi[i].style.margin='0'; } };