使用JavaScript 对同一个按钮点击打开,点击关闭 // using a button to close or open a div
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>test</title> <style> body{ padding: 0; margin: 0; } #box{ width: 200px; height: 0; background: #00f; overflow: hidden; } </style> <script> window.onload = function(){ var box = document.getElementById('box'); var btn = document.getElementById('btn'); btn.onclick = function(){ var height = box.offsetHeight; if(height == 0){ box.style.height = 100+'px'; }else{ box.style.height = 0; } } } </script> </head> <body> <div id="box"> <h2>Hello World</h2> </div> <button id="btn">点击展开关闭</button> </body> </html>