Ver Mensaje Individual
  #5 (permalink)  
Antiguo 09/02/2013, 07:21
Avatar de emprear
emprear
Colaborador
 
Fecha de Ingreso: junio-2007
Ubicación: me mudé
Mensajes: 8.388
Antigüedad: 16 años, 10 meses
Puntos: 1567
Respuesta: Problema con javascript y display

El problema es que con
elemento.style.display, solo podés recuperar el valor se la propiedad css es declarada en el atributo style, en el caso de querer saber un valor definido dentro de
<style>
#capa{
display: none;
}
</style>

debés usar el método window.getComputedStyle

Ejemplo
Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <title>titulo</title>
  5. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  6.  
  7. <style type="text/css">
  8. /*<![CDATA[*/
  9.  
  10. #hola{
  11. color: #000000;
  12. }
  13.  
  14. /*]]>*/
  15. <script type="text/javascript">
  16. //<![CDATA[
  17. /* script */
  18. function menus_desplegable() {
  19. var d=document.getElementById("hola");
  20. alert(d.style.display);
  21. alert(d.style.color);
  22. }
  23.  
  24.  
  25.  
  26. function menus_desplegable2(elemento,propiedad){
  27. var d = document.getElementById(elemento);
  28. if (d.currentStyle){// ie<9
  29. var valorPropiedad = d.currentStyle[propiedad];
  30. }else if (window.getComputedStyle){
  31. var valorPropiedad = document.defaultView.getComputedStyle(d,null).getPropertyValue(propiedad);
  32. }
  33. alert(d.style.display);
  34. alert(valorPropiedad);
  35. }
  36.  
  37.  
  38. //]]>
  39. </script>  
  40. </head>
  41. <button onclick="menus_desplegable();">ver display y color div hola</button>
  42. <button onclick="menus_desplegable2('hola','color');">ver display y color div hola, método 2</button>
  43. <div class="menus_desplegable" id="hola" style="display:none;">Descripción</div>
  44. </body>
  45. </html>

Saludos
__________________
La voz de las antenas va, sustituyendo a Dios.
Cuando finalice la mutación, nueva edad media habrá
S.R.