2009-10-06 85 views
0

根据大写金额的<div clas="product"></div>我正在尝试设置宽度.productholder计算宽度与jQuery

<div class="productholder"> 
    <div clas="product"></div> 
    <div clas="product"></div> 
    <div clas="product"></div> 
    <div clas="product"></div> 
    <div clas="product"></div> 
</div> 

我可以设置固定宽度。产品如果这会有所帮助。

我已经试过这一点,但不要似乎工作:

var width = 0; 
$('.product').each(function() { 
    width += $(this).outerWidth(true); 
}); 
$('.productholder').css('width', width + 250); 

感谢您对您的帮助提前。

+0

在''class''class'中缺少's'' – 2009-10-06 22:43:08

回答

2

您是否试过指定单位?

var width = 0; 
$('.product').each(function() { 
    width += $(this).outerWidth(true); 
}); 
$('.productholder').css('width', width + 250 + 'px'); 

顺便说一句,<div class="productholder">宽度将增长,以适应内<div>元素

的宽度

编辑:

您的代码不作为就是工作 - Working Demo。将/编辑添加到URL以查看代码。

是否<div class="productholder">有任何文字内容?你指定一个高度吗?

+0

我试过了。 Div在相对容器中是绝对的。产品本身是浮动的; Div不会延长,因为持有它的div具有固定宽度 – Dom 2009-10-06 22:48:19

+0

我的愚蠢!抱歉。在.js文件中找到了一些东西。非常感谢您的帮助! – Dom 2009-10-06 23:01:28

+0

没有probs,很高兴帮助:) – 2009-10-06 23:04:13

1

这项工作适合你吗?

var holder = $('.productholder'); 
var width = holder.find('.product').length * 250; 
holder.css('width', width + "px");