<!DOCTYPE html> <html lang="en"> <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>Document</title> <style> #box{ width:150px; height:150px; background-color:red; } </style> </head> <body> <div id="box"> </div> <input type="button" onclick="color()" value="变色"> <input type="button" onclick="w()" value="变宽"> <input type="button" onclick="h()" value="变高"> <input type="button" onclick="block()" value="显示"> <input type="button" onclick="none()" value="隐藏"> <script> var box = document.getElementById('box'); function color(){ box.style.backgroundColor="blue"; } function w(){ box.style.width="300px"; } function h(){ box.style.height="300px"; } function block(){ box.style.display="block"; } function none(){ box.style.display="none"; } </script> </body> </html>
通过getElementByID来获取要操作的div,然后通过style进行更改样式