7.获取元素的节点.html 951 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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. list-style: none;
  12. text-decoration: none;
  13. box-sizing: border-box;
  14. }
  15. #box {
  16. width: 300px;
  17. height: 400px;
  18. background: pink;
  19. margin-top: 100px;
  20. margin-left: 200px;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div id="box"></div>
  26. <script>
  27. var box = document.getElementById("box");
  28. console.log(box);
  29. // 获取元素宽度
  30. console.log(box.offsetWidth)
  31. // 获取元素高度
  32. console.log(box.offsetHeight)
  33. // 获取元素距离顶部的高度
  34. console.log(box.offsetTop)
  35. // 获取元素距离左侧的距离
  36. console.log(box.offsetLeft)
  37. </script>
  38. </body>
  39. </html>