jQuery - 设置div宽度为它的孩子的总和 [英] jQuery - set div width to sum of it's children

查看:89
本文介绍了jQuery - 设置div宽度为它的孩子的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下设置:



html:

  ; div id =holder> 
< div>
< img />
< img />
< / div>
< / div>

css

  #holder {width:800px; } 

现在,我想使jQuery库,并将移动图像的左右行



但是,我必须告诉< img /> s向左浮动,并将内部div的 width 设置为图像宽度的总和。


$



谢谢

解决方案

  var sum = 0; 
$('#holder img')每个(function(){sum + = $(this).width();});
$('#holder> div')。width(sum);

// alert(sum);

这应该可以做。



浮动到图像,你可以应用它与css

  #holder img {float:left;} 


I have following setup:

html:

<div id="holder">
  <div>
    <img/>
    <img/>
  </div>
</div>

css

#holder { width: 800px; }

now, i want to make jQuery gallery and will move line of images to the left and right with negative margin-left of inner div.

However, I have to tell <img/>s to float to left and set inner div's width to sum of widths of images.

How can I do this some clean and nice way?

thank you

解决方案

var sum=0;
$('#holder img').each( function(){ sum += $(this).width(); });
$('#holder > div').width( sum );

//alert(sum);

this should do the trick ..

The float to the images, you can apply it with css

#holder img{float:left;}

这篇关于jQuery - 设置div宽度为它的孩子的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆