7.获取元素宽度.html 804 B

12345678910111213141516171819202122232425262728
  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: 300px;
  10. height: 200px;
  11. border:2px solid #000;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div id="box"></div>
  17. <script>
  18. var box = document.getElementById("box");
  19. console.log(box.offsetWidth);//xxx.offsetWidth 元素宽度
  20. console.log(box.offsetHeight);//xxx.offsetHeight 元素高度
  21. box.onclick = function(event) {
  22. console.log(event.clientX,event.clientY);
  23. // clientX 当前元素点击时x轴位置坐标
  24. // clientY 当前元素点击时y轴位置坐标
  25. }
  26. </script>
  27. </body>
  28. </html>