Jquery: Ocultar todos los hijos, luego mostrar un elemento específico
Quiero ocultar todos los elementos secundarios en un div. Y luego mostrar uno específico pasado a la función.
function subDisplay(name) {
$("#navSub").each(function() {
$(this).hide();
});
$(name).show();
}
Luego llamo a la función desde un evento onmouse como: subDisplay(#DivIwantToShow);
Pero nada se muestra...
¿Qué estoy haciendo mal?
6 answers
Necesita ocultar los hijos y no el div que contiene.
$("#navSub").children().hide();
Así que ahora si el div que está tratando de mostrar es un elemento en el div padre, todavía se mostrará mientras los demás permanecen ocultos.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-05 14:57:04
Si estás apuntando a los hijos de #navSub
, necesitas dirigirlos y ocultarlos , en lugar del elemento navSub
; lo cual puedes hacer usando el children()
método;
function subDisplay(name) {
$('#navSub').children().hide();
$(name).show();
};
De lo contrario, parece que tiene varios elementos con el mismo ID en su DOM, lo que no está permitido.
Luego debe pasar una cadena (que es un selector de jQuery válido) a subDisplay()
;
subDisplay('#DivIwantToShow');
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-05 14:52:54
Para resumir los grandes comentarios de @dotweb y @Matt;
function subDisplay(name) {
$('#navSub').hide();
$(name).show();
}
subDisplay('#DivIwantToShow');
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-05 14:52:53
Si el nombre del elemento se pasa en name use esto:
if($(this).attr('name') != name){
//Hide it
} else {
//show it
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-05 14:56:33
function subDisplay(name) {
$("#navSub").hide();
$('#'+name).show();
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-05 14:51:58
Intenta tenerlo fuera del bucle, así:
function subDisplay(name) {
$("#navSub").hide();
$(name).show();
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-05 14:52:16