<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>JS控制DIV样式</title> <style> </style> </head> <body> <div id="box"></div> <input type="button" value="变高" onclick="changeh(this)"> <input type="button" value="变宽" onclick="changew(this)"> <input type="button" value="变色" onclick="changec(this)"> <input type="button" value="重置" onclick="reset(this)"> <input type="button" value="隐藏" onclick="hide(this)"> <input type="button" value="显示" onclick="show(this)"> <script> var box; window.onload = function() { box = document.getElementById("box"); } function changeh() { box.style.height = "300px"; } function changew() { box.style.width = "300px"; } function changec() { box.style.background = "#000"; } function reset() { box.style = "reset"; } function hide() { box.style.display = "none"; } function show() { box.style.display = "block"; } </script> </body> </html>
多多练习,重置样式也可以使用reset。