8.获取元素节点.html 901 B

12345678910111213141516171819202122232425262728293031323334
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. box-sizing: border-box;
  12. }
  13. .box {
  14. width: 300px;
  15. height: 400px;
  16. margin-top: 40px;
  17. margin-left: 200px;
  18. background: aqua;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div class="box"></div>
  24. <script>
  25. var box = document.querySelector(".box");
  26. console.log(box);
  27. console.log(box.offsetWidth); // 获取元素宽度
  28. console.log(box.offsetLeft); // 获取元素距离左侧的距离
  29. console.log(box.offsetTop); // 获取元素距离顶部的距离
  30. console.log(box.offsetHeight); // 获取元素高度
  31. </script>
  32. </body>
  33. </html>