博客列表 >JavaScript控制DIV样式

JavaScript控制DIV样式

刘静的博客
刘静的博客原创
2020年08月18日 13:02:58735浏览

JavaScript控制DIV样式代码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>01.JavaScript控制DIV样式</title>
  6. <style type="text/css" media="screen">
  7. #box{width:100px;height:100px;background:red;margin:20px 80px;}
  8. </style>
  9. </head>
  10. <body>
  11. <div id="box">
  12. </div>
  13. <input type="button" value="变高" onclick="aa(this)">
  14. <input type="button" value="变宽" onclick="bb(this)">
  15. <input type="button" value="变色" onclick="cc(this)">
  16. <input type="button" value="重置" onclick="dd(this)">
  17. <input type="button" value="隐藏" onclick="ee(this)">
  18. <input type="button" value="显示" onclick="ff(this)">
  19. <script type="text/javascript">
  20. var box;
  21. window.onload=function(){
  22. box=document.getElementById('box')
  23. }
  24. function aa(){
  25. box.style.height="400px";
  26. }
  27. function bb(){
  28. box.style.width="400px";
  29. }
  30. function cc(){
  31. box.style.background="blue";
  32. }
  33. function dd(){
  34. box.style.height="100px";
  35. box.style.width="100px";
  36. box.style.background="red";
  37. }
  38. function ee(){
  39. box.style.display="none";
  40. }
  41. function ff(){
  42. box.style.display="block";
  43. }
  44. </script>
  45. </body>
  46. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议