2012-03-23 20 views

Respuesta

46

Uso length

if ($('#example').find('#test').length) { 
    // found! 
} 
4

Creo que lo que busca es la siguiente

if (jQuery.contains($('#example'), $('#test')) { 
    $('#another').hide(); 
} 

Esto podría funcionar tan bien, no está seguro de la parte superior de la cabeza.

if ($('#test', $('#example')).size() > 0) { 
    $('#another').hide(); 
} 
+1

'tamaño()'. ¡No he visto eso en mucho tiempo! 'length' ftw. – elclanrs

+4

¿Debería llamarse 'contains' con un elemento DOM como parámetro y no como un objeto jQuery? http://api.jquery.com/jQuery.contains/ – mxro

3
<script> 
    var test = $('#example #test').length(); 
    if(test !== 0){ 
     $('#another').hide(); 
    } 
</script> 
+0

'test' nunca será' indefinido' – Esailija

+1

La longitud no es una función -> 'longitud'. Y también es falso, así que puedes deshacerte de '! == 0' y simplemente hacer' if (test) ' – elclanrs

1

es bastante simple.

$(document).ready(function(){ 
    if($('#example').children('#test').length > 0) 
    { 
     $('#another').hide(); 
    } 
});​ 

http://jsfiddle.net/8rrTp/

Cuestiones relacionadas