按钮单击更改div高度

Jim*_*Jim 16 html javascript

我做错了什么?为什么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>
Run Code Online (Sandbox Code Playgroud)

Ari*_*lam 34

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


Sud*_*oti 12

做这个:

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