2

我有一个这样的列表:

<div id="post-1" class="post"></div>
<div id="post-2" class="post"></div>
<div id="post-3" class="post"></div>
<div id="post-4" class="post"></div>
<div id="post-5" class="post"></div>

我想选择#post-1 AND #post-2,然后做点什么。然后选择#post-3 AND #post-4 并做某事,然后抓住最后一个并做某事。这将超过很多 div,所以我需要它是某种循环。

伪代码是这样的:

var maxHeight = 0;
jQuery('.post-block:two-at-time').each(function(){
     var height = jQuery(this).height();
     if( height > maxHeight) {
          maxHeight = height;
     }
});
jQuery('.post-block:two-at-time').height(maxHeight);

关于如何最好地做到这一点的任何想法?谢谢!

4

4 回答 4

2
var $post = $('.post'),
    $even = $post.filter(':even'),
    $odd = $post.filter(':odd')

    for (var i = 0; i < $even.length; i++) {
        var $a = $even.eq(i).add( $odd.eq(i) );
        // ... ?
    }

http://jsfiddle.net/E3kbP/

于 2013-02-07T00:19:41.723 回答
1

这可能不是最干净的方式,但您可以使用普通for循环来迭代您的最小值/最大值,然后使用.slice

for (var start = 0, end = 2; start < $(".post").length; start += 2, end += 2) {
    jQuery('.post').slice(start, end).each(function () {

http://jsfiddle.net/ExplosionPIlls/EJY5W/

于 2013-02-07T00:11:51.633 回答
0

也许它会这样工作..

for( var id = 2; id < $('.post').length; id=id+2){
     var height = $('post-'+(id-1)).height() + $('post-'+id).height(); // I guess this way should work
     if( height > maxHeight) {
          maxHeight = height;
     }
});
于 2013-02-07T00:21:14.873 回答
0

这也会将.post元素按两个分组,查找正确的一个maxHeight,然后将所有元素的高度设置为maxHeight..

var count = maxHeight = 0;
$('.post').each(function ()
{
    if (count % 2 == 0)
    {
        var even = $(this).height(); odd = $(this).next('div').height();
        if(even > maxHeight || odd > maxHeight)
            (even >= odd) ? maxHeight = even : maxHeight = odd;
    }
    count++;
}).css('height', maxHeight);
于 2013-02-07T00:47:12.817 回答