5.获取事件元素.html 740 B

1234567891011121314151617181920212223242526
  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. .box {
  9. width: 210px;
  10. height: 220px;
  11. background: #f00;
  12. /* margin-left: 30px; */
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div class="box"></div>
  18. <script>
  19. var box = document.querySelector(".box");
  20. console.log(box.offsetWidth); //获取元素宽度
  21. console.log(box.offsetHeight); // 获取元素高度
  22. console.log(box.offsetLeft); //获取元素距离左侧的距离
  23. console.log(box.offsetTop); //获取元素距离顶部的距离
  24. </script>
  25. </body>
  26. </html>