2012-04-16 16 views

risposta

31
$('.test:not(:has(.example))') 

oppure

$('.test').not(':has(.example)') 
+0

Questo ha funzionato al meglio, grazie! – UserIsCorrupt

1

è possibile utilizzare il metodo children con ".example" e verificare se è vuota

1

jQuery :

jQuery.contains(document.documentElement, document.body); // true 
jQuery.contains(document.body, document.documentElement); // false 
4

Possibilmente

$('.test').filter(function() { return !$(this).children('.example').length; }); 

Questo filtra qualsiasi elementi che hanno qualsiasi bambino che corrisponde a .example. Se si desidera filtrare in base ai discendenti (non solo ai bambini), è possibile sostituire .find per .children.

1
$('.test').each(function() { 
    if(!$(this).children().hasClass("example")){ 
     //your code 
    } 
}); 

Forse in questo modo? Non ho ancora testato questo ...

2
$(':not(.test:has(.example))').css('color', 'red');​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ 

http://jsfiddle.net/DZZUZ/2/

2

Questo problema sembra ready-made per la funzione filter dove trovare tutte le .test oggetti e quindi quando filtrano conservano solo quelli che non hanno .example in loro:

$(".test").filter(function() { 
    return($(this).find(".example").length == 0); 
}); 
-1
if ($('#yourDiv').children().hasClass("className")) { 
    // yourDivID' has any children whose class name =>'className' 
} 
Problemi correlati