2016-07-13 61 views
0

我想用jquery做过滤器,首先我隐藏所有的div标签,然后尝试只显示选定的div标签,我选择他们在一个变量,并检查收集的div的长度是正确的,但无法循环并向他们展示。用jquery过滤

var filter = $('.sec5row.'+classtype); //this is the selected variable 
//I try to loop it and show 
for (var i = 0; i < = filter.length; i++){ 
     $(filter[i]).show(); 
} 


but then I do filter[1].show() it works 

请告诉我正确的方式来循环它。

回答

4

你并不需要一个循环都:

$('.sec5row.' + classtype).show(); 

jQuery objects are collections。当您调用诸如show之类的方法时,它将应用于对象中包含的所有元素。

4

你并不需要遍历元素只是告诉他们:

$('.sec5row.'+classtype).show(); 
1

使用此:

$('.sec5row.'+classtype).each(function(this){ 

//do whatever you want to do in this loop, filtering, display, checking properties etc..$(this) will give you the element selector 
$(this).show(); 

    });