61,112
社区成员
发帖
与我相关
我的任务
分享
<script>
function doCenter(){
var Mleft= '10px';
var tmpWidthC= '600px';
var tmpWidthC1= '600px';
var tmpWidthC2= '600px';
var tmpWidthC3= '600px';
tmpWidthC = document.getElementById('Content').style.width;
tmpWidthC1 = document.getElementById('Content_1').style.width;
tmpWidthC2 = document.getElementById('Content_2').style.width;
tmpWidthC3 = document.getElementById('Content_3').style.width;
tmpWidthC = parseInt(tmpWidthC.replace('px',''));
tmpWidthC1 = parseInt(tmpWidthC1.replace('px',''));
tmpWidthC2 = parseInt(tmpWidthC2.replace('px',''));
tmpWidthC3 = parseInt(tmpWidthC3.replace('px',''));
Mleft = parseInt((tmpWidthC-tmpWidthC1-tmpWidthC2-tmpWidthC3)/4); //DIV float之后margin翻倍,所以多除了一次2
alert(Mleft);
document.getElementById('Content_1').style.marginLeft = Mleft+'px';
}
</script>
<center>
<div style="background:#ff0000; text-align:center; width:650px;" ID="Content">
<div style="height:50px; width:50px; float:left; background:#cc1; margin-left:0px;" ID="Content_1">a </div>
<div style="height:50px; width:60px; float:left; background:#c2c;" ID="Content_2">b </div>
<div style="height:50px; width:70px; float:left; background:#3cc;" ID="Content_3">c </div>
</div>
</center>
<script>doCenter();</script>