1

我有一个脚本,它在 3 列中获取一些浮动 div,从 1 行的 3 列中获取最大高度,并使所有 3 个 div 的高度相同。然后它对每一行重复它,脚本工作正常但是我在 JQuery 中构建了一个自定义过滤器系统,根据价格范围或品牌等的选择隐藏或显示某些 div。

当我使用过滤器时,它将删除第一行的第 2 列,并使第二行的第 4 列向上移动到第一行。将第 2 列留在代码中,因此重新运行高度/列检查脚本将不起作用。

所以我想要做的是我在哪里使用我的filter(':nth-child(3n-2)')添加 if 语句,它将跳过所有从 :nth-child 选择中隐藏的 div。

我确实知道我可以在我的内部使用一个函数,filter();但它对我来说是一个新函数,所以我不确定如何准确地使用它。

这是我的工作代码:

var $sections = $('.section-link');
$sections.filter(':nth-child(3n-2)').each(function () {
    var $this = $(this),
        $els = $this.nextAll(':lt(2)').addBack();
    var sectionheight = new Array();
    $els.each(function () {
        var value = $(this).find('.section-title').height();
        sectionheight.push(value);
    });
    var newsectionheight = Math.max.apply(Math, sectionheight);
    $els.find('.section-title').height(newsectionheight);
})

JSfiddle 示例

到目前为止,我正在尝试使用这样的东西:

$sections.filter(function() {
   return $(this).css('display') !== 'none';
}, ':nth-child(3n-2)')
4

1 回答 1

0

好的,通过在调用 :nth-child 之前返回 css 属性来使其工作

function makeheight(){
var $sections = $('.section-link');

    $sections.filter(function() {
   return $(this).css('display') == 'block';
}, ':nth-child(3n-2)').each(function () {

        var $this = $(this),
            $els = $this.nextAll(':lt(2)').addBack();
        var sectionheight = new Array();
        $els.each(function () {
            var value = $(this).find('.section-title').height();
            sectionheight.push(value);
        });
        var newsectionheight = Math.max.apply(Math, sectionheight);
        $els.find('.section-title').height(newsectionheight);
    });


}
于 2013-09-18T11:58:45.063 回答