0

此处提供完整上下文:http: //jsfiddle.net/dweiliu/bcHsy/1/

我目前有 2 个部分,其中 2 个表都嵌套在自己的文章中,如下所示:

<section>
    <article>
        <h2/>
        <table>...</table>
    </article>
    <article>
        <h2/>
        <table>...</table>
    </article>
</section>
<section>
    <article>
        <h2/>
        <table>...</table>
    </article>
    <article>
        <h2/>
        <table>...</table>
    </article>
</section>

我需要这种行为来处理每个部分的 3 篇文章等等......

$('section').each(function(){
    var heights = [];
    $('article').each(function(){
        heights.push($(this).find('table tr').length);
    });
    maxHeight = Math.max.apply(Math, heights);
    calculatedHeight = maxHeight * 35 + 35;
    $('article').css('height',calculatedHeight);
});

我希望能够浏览每个部分,查看该部分中的表格,找到最长的表格 ('table tr').length 并根据该长度计算高度。

现在,代码只是遍历整个页面,并且在完成任何给定部分后不会重置数组高度的最大值。我知道我错过了一些东西。有人可以指出我正确的方向吗?

4

1 回答 1

3

I assume your articles are within the sections. If that's so, you need to select your articles within the context of each section:

$('section').each(function(){
    var heights = [];
    $('article', this).each(function(){
        heights.push($(this).find('table tr').length);
    });
    maxHeight = Math.max.apply(Math, heights);
    calculatedHeight = maxHeight * 35 + 35;
    $('article', this).css('height',calculatedHeight);
});

The differences are probably negligible, but you might see if this alternative is faster:

$('section').each(function(){
    var max = 0;
    $('article', this).each(function(){
        max = Math.max(max, $(this).find('table tr').length);
    });
    var calculatedHeight = max * 35 + 35;
    $('article', this).css('height',calculatedHeight);
});
于 2013-08-06T19:58:48.043 回答