2013-03-27 49 views
0

如何获取此功能恢复显示所有过滤器被删除时的一切?恢复显示当所有过滤器被删除时全部

我试图改变if (attrColor == 'All') {...}if (attrColor == 'All' || attrColor == '') {...}

我已经贴了拨弄着这里的功能的一个简单的例子:http://jsfiddle.net/chayacooper/WZpMh/88/

$(document).ready(function() { 
    var selected = []; 
    $('#attributes-Colors *').click(function() { 
     var attrColor = $(this).data('color'); 
     var $this = $(this); 
     if ($this.parent().hasClass("active")) { 
      $this.parent().removeClass("active"); 
      selected.splice(selected.indexOf(attrColor),1); 
     } 
     else { 
      $this.parent().addClass("active"); 
      selected.push(attrColor); 
     } 
     if (attrColor == 'All') { 
      $('#content').find('*').show(); 
     } else { 
     $("#content").find("*").hide(); 
     $.each(selected, function(index,item) { 
      $('#content').find('[data-color *="' + item + '"]').show(); 
     }); 
     } 
     return false; 
    }); 
}); 
+0

不能看到你摆弄工作。得到:页面不能显示在输出部分?? – Sangeeta 2013-03-27 06:45:17

+0

@Sangeeta - 在我的电脑上工作正常,尽管我只在Firefox上试过。您是否使用其他浏览器? – 2013-03-27 06:55:10

回答

1

看到这个:Demo

if (attrColor == 'All' || !selected.length) { 
    $('#content').find('*').show(); 
} else { 
    $("#content").find("*").hide(); 
    $.each(selected, function (index, item) { 
     $('#content').find('[data-color *="' + item + '"]').show(); 
    }); 
} 

编辑:

更新按照@ ArunPJohny的建议:

使用.children()选择会比*选择更好的让孩子li小号

if (attrColor == 'All' || !selected.length) { 
    $('#content').children().show(); 
} else { 
    $("#content").children().hide(); 
    $.each(selected, function (index, item) { 
     $('#content').find('[data-color *="' + item + '"]').show(); 
    }); 
} 
+1

作为一个补充建议,最好不要使用selecot'*',参见演示http://jsfiddle.net/arunpjohny/KnKYc/ – 2013-03-27 06:50:48

+0

@ArunPJohny - 你介绍一下为什么你不推荐使用*选择器? – 2013-03-27 06:56:45

+0

@ArunPJohny,很好建议.. – Anujith 2013-03-27 06:57:22

相关问题