2011-11-24 109 views
12

我做错了什么?为什么div高度没有改变?按钮单击更改div高度

<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

你应该熟悉jQuery的,它使事情比较容易的方式。 :) www.jquery.com – odaa

回答

24

只是一个愚蠢的错误使用引号( '')在'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

这样做:

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

1

您必须时使用的像素设置高度为字符串值。

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

也尝试添加一个DOCTYPE到您的HTML为Internet Explorer。

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

只是删除了 “PX” 在style.height分配,如:

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

应该

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

你忘记了引号。根据此更改您的代码:

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

应该工作。

1

看向vwphillips'从03-06-2010后,下午3时35分在 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> 

这对我有效:)

是问候!

+0

请注意,[链接只是答案](http://meta.stackoverflow.com/tags/link-only-answers/info)不鼓励,所以答案应该是一个终点寻找一个解决方案(而不是另一个引用的中途停留,这往往会随着时间推移而变得陈旧)。请考虑在此添加独立的摘要,并将链接保留为参考。 – kleopatra

+0

我将更新我的答案......谢谢@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>