2010-11-14 7 views

Répondre

3

Vous pouvez le faire à l'intérieur avec une négation ! comme ceci:

$("input").each(function() { 
    if (!$(this).hasClass('valid')) { 
     // Do something 
    } 
}); 

Ou il suffit d'utiliser :not() lorsque vous sélectionnez tho éléments proprement dits, comme celui-ci:

$("input:not(.valid)").each(function() { 
    // Do something 
}); 

Cela signifie que votre code d'origine peut aussi être plus mince (quand il ne ont la classe), comme celui-ci:

$("input.valid").each(function() { 
    // Do something 
}); 
1

Utilisez l'opérateur négatif !:

$("input").each(function() { 
    if (!($(this).hasClass('valid'))) { // pass this statement if the valid class is not present 
     // Do something 
    } 
}); 
2

Vous pouvez également utiliser le :not selector

$("input:not(.valid)").each(function() { 
    //Do Something 
});