javascript - 过滤器 :nth-child ignoring where css display is none

标签 javascript jquery

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

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

所以我想做的是使用我的 filter(':nth-child(3n-2)') 添加一个 if 语句,该语句将跳过所有隐藏的 div :nth-child 选择。

我确实知道我可以在 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 example

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

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

最佳答案

好的,在调用 :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);
    });


}

关于javascript - 过滤器 :nth-child ignoring where css display is none,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18871393/

相关文章:

javascript - 单击页面的关闭按钮时,我想清除所有选择框值

javascript - React-Native Metro bundler 无法提供任何文件

javascript - 将源设置为天蓝色媒体播放器

c# - 如何操纵回发优先级?

javascript - Node.js Express 接收 postman 为空

javascript - 如何更改外部 .js 文件中的变量数据值?

javascript - HTML5 视频 - 如何检测视频的特定部分何时加载

javascript - AcceptVerbs 在 Web API Controller 中不起作用

jquery - 如何使用 JSPM 填充 jQuery 插件?

javascript - 缩短 jQuery 命令