0

我有这个

<div class="projekt">
<div id="main" class="home" role="main"> 
  <div class="slide"><img  src="http://tribulant.net/lightbox/files/2010/08/image-2.jpg" /></div><div class="slide"><img src="http://cyears.files.wordpress.com/2011/04/image4.jpg" /></div>
  <div class="slide"><img  src="http://tribulant.net/lightbox/files/2010/08/image-2.jpg" /></div><div class="slide"><img src="http://cyears.files.wordpress.com/2011/04/image4.jpg" /></div>
  <div class="slide"><img  src="http://tribulant.net/lightbox/files/2010/08/image-2.jpg" /></div><div class="slide"><img src="http://cyears.files.wordpress.com/2011/04/image4.jpg" /></div>
  <div class="slide"><img  src="http://tribulant.net/lightbox/files/2010/08/image-2.jpg" /></div><div class="slide"><img src="http://cyears.files.wordpress.com/2011/04/image4.jpg" /></div>
  <div class="slide"><img  src="http://tribulant.net/lightbox/files/2010/08/image-2.jpg" /></div><div class="slide"><img src="http://cyears.files.wordpress.com/2011/04/image4.jpg" /></div>
  <div class="slide"><img  src="http://tribulant.net/lightbox/files/2010/08/image-2.jpg" /></div><div class="slide"><img src="http://cyears.files.wordpress.com/2011/04/image4.jpg" /></div>
</div>
</div>
<div class="projekt">
<div id="main" class="home" role="main"> 
  <div class="slide"><img  src="http://tribulant.net/lightbox/files/2010/08/image-2.jpg" /></div><div class="slide"><img src="http://cyears.files.wordpress.com/2011/04/image4.jpg" /></div>
  <div class="slide"><img  src="http://tribulant.net/lightbox/files/2010/08/image-2.jpg" /></div><div class="slide"><img src="http://cyears.files.wordpress.com/2011/04/image4.jpg" /></div>
  <div class="slide"><img  src="http://tribulant.net/lightbox/files/2010/08/image-2.jpg" /></div><div class="slide"><img src="http://cyears.files.wordpress.com/2011/04/image4.jpg" /></div>
  <div class="slide"><img  src="http://tribulant.net/lightbox/files/2010/08/image-2.jpg" /></div><div class="slide"><img src="http://cyears.files.wordpress.com/2011/04/image4.jpg" /></div>
  <div class="slide"><img  src="http://tribulant.net/lightbox/files/2010/08/image-2.jpg" /></div><div class="slide"><img src="http://cyears.files.wordpress.com/2011/04/image4.jpg" /></div>
  <div class="slide"><img  src="http://tribulant.net/lightbox/files/2010/08/image-2.jpg" /></div><div class="slide"><img src="http://cyears.files.wordpress.com/2011/04/image4.jpg" /></div>
</div>
</div>

和jQuery:

var accum_width = 0;
$('.home').find('.slide').children('img').each(function() {
   accum_width += $(this).width();
});
$('.projekt').width(accum_width);
$('.home').width(accum_width);

如何制作,它将为每个 .project 分别添加图像的宽度。现在它正在添加页面上所有图像的宽度。

4

1 回答 1

1

为每个 .projekt(或 .home)类分别添加宽度,如下所示:

$('.home').each(function () {
    var accum_width = 0;
    $(this).find('.slide').children('img').each(function () {
        accum_width += $(this).width();
    });
    $(this).width(accum_width);
});

​</p>

于 2012-07-18T16:05:33.653 回答