我有一个脚本,它在 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/