2011-11-24 10 views
12

¿Qué hago mal? ¿Por qué la altura de div no cambió?Cambiar la altura del div en el botón, haga clic en

<html> 
    <head>  
    </head> 

<body > 
    <button type="button" onClick = "document.getElementById('chartdiv').style.height = 200px">Click Me!</button> 
    <div id="chartdiv" style="width: 100%; height: 50px; background-color:#E8EDF2"></div> 
</body> 
</html> 
+1

usted debe familiarizarse con jQuery, que hace las cosas manera más fácil. :) www.jquery.com – odaa

Respuesta

24

Sólo una cotización utilización error tonto ('') en '200px'

<html> 
    <head>  
    </head> 

<body > 
    <button type="button" onClick = "document.getElementById('chartdiv').style.height = '200px';">Click Me!</button> 
    <div id="chartdiv" style="width: 100%; height: 50px; background-color:#E8EDF2"></div> 
</body> 

9

Haga lo siguiente:

 
function changeHeight() { 
document.getElementById('chartdiv').style.height = "200px" 
} 
<button type="button" onClick="changeHeight();"> Click Me!</button> 

1

usted tiene que fijar la altura como un valor de cadena cuando se utiliza píxeles.

document.getElementById('chartdiv').style.height = "200px" 

También intente agregar un DOCTYPE a su HTML para Internet Explorer.

<!DOCTYPE html> 
<html> ... 
-1

Basta con retirar el "px" en la asignación style.height, como:

<button type="button" onClick = "document.getElementById('chartdiv').style.height = 200px"> </button> 

Debe ser

<button type="button" onClick = "document.getElementById('chartdiv').style.height = 200">Click Me!</button> 
0

Sólo olvidado las comillas. Cambie su código de acuerdo con esto:

<button type="button" onClick = "document.getElementById('chartdiv').style.height = '200px'">Click Me!</button> 

debería funcionar.

1

Mira a vwphillips' post de 03-06-2010, 15:35 en http://www.codingforums.com/archive/index.php/t-190887.html

<html> 
    <head> 
     <title></title> 
     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 

     <script type="text/javascript"> 

     function Div(id,ud) { 
      var div=document.getElementById(id); 
      var h=parseInt(div.style.height)+ud; 
      if (h>=1){ 
       div.style.height = h + "em"; // I'm using "em" instead of "px", but you can use px like measure... 
      } 
     } 
     </script> 

    </head> 
    <body> 
     <div> 
     <input type="button" value="+" onclick="Div('my_div', 1);">&nbsp;&nbsp; 
     <input type="button" value="-" onclick="Div('my_div', -1);"></div> 
     </div> 

     <div id="my_div" style="height: 1em; width: 1em; overflow: auto;"></div> 

    </body> 
</html> 

Esto funcionó para mí :)

Be ¡Saludos!

+0

Tenga en cuenta que [las respuestas solo de enlace] (http://meta.stackoverflow.com/tags/link-only-answers/info) no se recomiendan, entonces las respuestas deberían ser el punto final de un buscar una solución (frente a otra escala más de referencias, que tienden a quedar obsoletas en el tiempo). Considere agregar una sinopsis independiente aquí, manteniendo el enlace como referencia. – kleopatra

+0

Voy a actualizar mi respuesta ... Gracias @kleopatra! :) – mailazs

0

var ww1 = ""; 
 
var ww2 = 0; 
 
var myVar1 ; 
 
var myVar2 ; 
 
function wm1(){ 
 
    myVar1 =setInterval(w1, 15); 
 
} 
 
function wm2(){ 
 
    myVar2 =setInterval(w2, 15); 
 
} 
 
function w1(){ 
 
ww1=document.getElementById('chartdiv').style.height; 
 
ww2= ww1.replace("px", ""); 
 
    if(parseFloat(ww2) <= 200){ 
 
document.getElementById('chartdiv').style.height = (parseFloat(ww2)+5) + 'px'; 
 
    }else{ 
 
     clearInterval(myVar1); 
 
    } 
 
} 
 
function w2(){ 
 
ww1=document.getElementById('chartdiv').style.height; 
 
ww2= ww1.replace("px", ""); 
 
    if(parseFloat(ww2) >= 50){ 
 
document.getElementById('chartdiv').style.height = (parseFloat(ww2)-5) + 'px'; 
 
    }else{ 
 
     clearInterval(myVar2); 
 
    } 
 
}
<html> 
 
    <head>  
 
    </head> 
 

 
<body > 
 
    <button type="button" onClick = "wm1()">200px</button> 
 
    <button type="button" onClick = "wm2()">50px</button> 
 
    <div id="chartdiv" style="width: 100%; height: 50px; background-color:#ccc"></div> 
 
    
 
    <div id="demo"></div> 
 
</body>

Cuestiones relacionadas