0

我有一个过滤器函数,它通过查找类并隐藏其他所有内容来使用 div 类。我还有一个动态添加 div 的函数,但它不能与 filter 函数一起正常工作。使用索引号将动态添加的 div 添加到每行的末尾。

问题是隐藏的 div 保留了它们原来的索引号。我想将它们全部分离(保留 div 的内容)并将它们添加到选定的 div 之后。

Example before filtering (A0 = filter/index):
A0  B1  A2
<Dynamic Div after Row 1>
A3  B4  B5

This is what happens now when I filter:
B1  B4  B5
A0  A2  A3
<Dynamic Div after Row 1 - because A2 is the end of row 1 according to the index>

This is what I need to happen:
B0  B1  B2
<Dynamic Div after Row 1 - index numbers were changed>
A3  A4  A5

这是我的过滤功能:

$('nav a').click(function(){
    var content = '.content';
    var portfolio = '#portfolio > a';
    var getClass = $(this).attr('class'); //get the filter via class

    //hides any active content windows
    if($(content).length) { $(content).slideUp(300, function(){ $(this).remove(); }); }

    if(getClass){
        $('.tags').not("." + getClass).slideUp(300, function() {     //hide all tags except selected tag
            $("." + getClass).slideDown(300);   //show selected tag if already hidden
            $(portfolio).children($('.tags').not("." + getClass).parent()).detach();
        });
    } else {
        $('.tags').slideDown(300); //show all
    }

});

这是我不正确的行:

$(portfolio).children($('.tags').not("." + getClass).parent()).detach();

我需要选择投资组合中的 .tags 以便不删除导航元素,但我需要选择该 div 的锚链接(父元素)。

Stackoverflow 不允许我发布我的元素的图像,我无法让 html 代码看起来正确,但基本上我的元素看起来像: a href="content.php" div class="tags branding" style="background:url(images/BacktoSchool-header.jpg)" div class="text" Content Name

4

1 回答 1

0

我在导航中更改了一行,使 name 属性保持类值,这样我就可以隐藏所有'.tags'而不删除导航元素或需要定位投资组合的子类,然后是类的父类。

工作代码的变化是:

var getClass = $(this).attr('name'); //get the filter via name

if(getClass){
    $('.tags').not("." + getClass).slideUp(300, function() {     //hide all tags except selected tag
        $("." + getClass).slideDown(300);   //show selected tag if already hidden
            $holder = $('.tags').not("." + getClass).parent().detach();  //detaches all the other tags
            $holder.insertAfter($(portfolio + ':last'))  //appends them
        });
    } else {
        $('.tags').slideDown(300); //show all
    }
于 2013-04-26T04:08:21.007 回答