The question is seen on the internet today.
To get the sum of the widths of all the div below #box, then assign to #box, no matter how many div you add, #box的宽都会随着div的增加而改变.
<style>#box{Display:Inline-block;Border:1px #333 Solid;} </style> <DivID= "box"> <Divstyle= "width:100px; height:50px;"></Div> <Divstyle= "width:100px; height:50px;"></Div> <Divstyle= "width:100px; height:50px;"></Div> <inputtype= "button"ID= "BTN"value= "Modify" /> </Div> <Pclass= "notice"></P> <Script> $("#btn"). Click (function(){ varW=0; $("#box Div"). each (function() {W+=parseint ($ ( This). width ()); }); $("#box"). Width (w);//or Use $ ("#box"). CSS ("width", W) the same $(". Notice"). HTML ("The total width of the div is:"+W); }); </Script>
The main thing is to use each, traverse all the div, get their width and add up.
Original reproduced from: http://www.santii.com/article/128.html
JS gets the total width of all child elements under the element assigned to the parent element