2012-05-17 34 views
1

我通过表使用$("table").each(function()的jQuery跳过具有特定CSS类

如何跳过有一个CSS类表“NotThisClass”穿越

$("table").each(function(){ 
    if($(this:not('.NotThisClass'))) 
    { 
     // Do Stuff 
    } 
    } 

”不是个表t工作,这也不是,

if($(this).not('.NotThisClass')) 

什么是正确的语法?

回答

7
$("table").not('.NotThisClass').each(function() { 
    //Do your stuff! 
}); 
+0

这看起来优雅,我给它的明天去。为了将来的参考,我怎样才能用'.NotThisClass'来做其他事情,而不是像传统的if else语句那样? – Holly

+0

@霍利 - 我不完全确定你指的是什么。 jQuery选择器为您提供了一组要处理的对象。如果有些元素你不想使用,那么最好将它们直接排除在选择器之外,这样你就不会遍历它们。如果你需要一些基于类的条件样式或其他东西,那么你可以有多个选择器,或者在迭代对象的时候使用像pankil thakkar指出的那样的'if'子句。 –

1
$("table").each(function(){ 
    if(!$(this).hasClass('NotThisClass'))) 
    { 
     // Do Stuff 
    } 
    } 
+0

我试过这个或类似的东西之前,我张贴当然我可以有一个错字 – Holly

4

试试这个:

$("table:not(.NotThisClass)").each(function() 
{ 
    // Do Stuff 
}); 
+0

短而简单 –