【发布时间】:2012-03-20 15:22:07
【问题描述】:
我正在使用以下内容在.content 中查找图像并将其宽度应用于.project 父宽度。
$('.content').find('img').each(function () {
var $this = $(this), width = $this.width();
{
$(this).closest('.project').css("width", width);
}
});
我的问题是它在.content 中找不到最大 图像,并且有时应用的宽度小于最大图像,这给我的布局造成了问题。
任何帮助都会很棒。谢谢。
编辑
糟糕,detail 错了,答案很棒!我只需要应用父project div 的最大宽度。
以 Sudhir 的回答为基础。
这不起作用,它应该起作用吗?
$(document).ready(function() {
var max_width = 0;
$('.content').find('img').each(function(){
var this_width = $(this).width();
if (this_width > max_width) max_width = this_width;
});
$(this).closest('.project').css('width', max_width + 'px');
});
布局示例。有很多项目。
<div class="container">
<div class="project">
<div class="content">
<img src="small.jpg" height="100" width="100" />
<img src="large.jpg" height="400" width="600" />
<img src="medium.jpg" height="400" width="600" />
</div>
<div class="meta">Other content here.
</div>
</div>
<div class="project">
<div class="content">
<img src="small.jpg" height="100" width="100" />
<img src="large.jpg" height="400" width="600" />
</div>
<div class="meta">Other content here.
</div>
</div>
【问题讨论】:
-
我无法想象您网站上 Div 的布局。你有很多 .content div,每个都有自己的 .project div?还是有一个 .content div,里面有 .project div,每个里面都有图片?您能否发布一个 HTML 布局的示例。
-
是的,每个 .content div 也有它自己的父 .project div。 @Tim 我希望这是有道理的,很多 .projects 在一个更大的容器中。