Javascript获取某一标签的座标并返回其座标对象

昨天搞了一个日历选择器的小东西,竟然发现以前用的获取标签的座标的函数在Firefox下不能执行,比较郁闷。于是花心思整理了一个,现此代码支持Ie,Firefox,Opera,记录于此!

javascript获取座标
  1. function getAbsPoint(e) {   
  2.     var x = e.offsetLeft;   
  3.     var y = e.offsetTop;   
  4.     while(e = e.offsetParent) {   
  5.         x += e.offsetLeft;   
  6.         y += e.offsetTop;   
  7.     }   
  8.     return {"x": x, "y": y};   
  9. }  

javascript获取座标使用方法
  1. <html>  
  2. <head>  
  3. <meta http-equiv=content-type content="text/html; charset=UTF-8">  
  4. <title>phplamp.org</title>  
  5. <script type="text/javascript">  
  6. function getAbsPoint(e) {   
  7.     var x = e.offsetLeft;   
  8.     var y = e.offsetTop;   
  9.     while(e = e.offsetParent){   
  10.         x += e.offsetLeft;   
  11.         y += e.offsetTop;   
  12.     }   
  13.     return {"x": x, "y": y};   
  14. }   
  15. function phplamp(e) {   
  16.     var xy = getAbsPoint(e);   
  17.     alert("offsetLeft=" + xy.x + "\noffsetTop=" + xy.y);   
  18. }   
  19. </script>  
  20. </head>  
  21. <body>  
  22. <input type="button" onclick="phplamp(this)" value="点击" />    
  23. </body>  
  24. </html>  
版块:javascript笔记 Tags: , 时间:2008-09-02
文章评论
0 回复 for "Javascript获取某一标签的座标并返回其座标对象"
phplamp - 2008-11-20
您好,您的评论将出现在这里!
评论表单