2010-03-10 78 views
0
<ul id ='caseStudies'> 
<li class="humor crime fantasy hidden"> A </li> 
<li class="crime"> B </li> 
<li class="humor crime hidden"> C </li> 
<li class="humor crime"> D </li> 
<li class="humor crime fantasy action hidden"> E </li> 
<li class="fantasy action"> F </li> 
<li class="humor fantasy"> G </li> 
<li class="crime action hidden"> H </li> 
</ul> 



$('ul#caseStudies li.hidden').each(function() { 

}//this will get all the LI in the UL that has got class 'hidden' 

但是我如何获得所有在UL中没有得到'隐藏'类的LI?如何获取jQuery中没有获得某个类的项目?

回答

4

尝试:not() selector

$('ul#caseStudies li:not(.hidden)').each(function() { 

} 
+0

@Andy有获得这些项目的计数的方法吗?我想使用,在每一个() – manraj82 2010-03-10 12:27:25

+0

更新我的答案。 – rahul 2010-03-10 12:32:02

+0

@ manraj82:set'var visibleCS = $('ul#caseStudies li:not(.hidden)');'然后使用'visibleCS.length'来获取查询发现的项目总数。 – 2010-03-10 12:38:09

3

使用:not选择

$('#caseStudies li:not(".hidden")') 

编辑

得到计数也

var notHiddenElems = $('#caseStudies li:not(".hidden")');. 
var notHiddenElemsLength = notHiddenElems.length; 

notHiddenElems.each(function(){ 
    // you can use notHiddenElemsLength here 
}); 

length

+1

大声笑击败了你2秒;-) – 2010-03-10 12:16:02

+0

+1的那个@Andy。 – rahul 2010-03-10 12:16:43

+0

@rahul:也为你+1。 – 2010-03-10 12:25:40

相关问题