首页 > 网站 > WEB开发 > 正文

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

2024-04-27 14:01:31
字体:
来源:转载
供稿:网友

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

javascript获取座标
function getAbsPoint(e) {   
    var x = e.offsetLeft;   
    var y = e.offsetTop;   
    while(e = e.offsetParent) {   
        x += e.offsetLeft;   
        y += e.offsetTop;   
    }   
    return {"x": x, "y": y};   
}  


Javascript获取座标使用方法
<html>  
<head>  
<meta http-equiv=content-type content="text/html; charset=UTF-8">  
<title>phplamp.org</title>  
<script type="text/javascript">  
function getAbsPoint(e) {   
    var x = e.offsetLeft;   
    var y = e.offsetTop;   
    while(e = e.offsetParent){   
        x += e.offsetLeft;   
        y += e.offsetTop;   
    }   
    return {"x": x, "y": y};   
}   
function phplamp(e) {   
    var xy = getAbsPoint(e);   
    alert("offsetLeft=" + xy.x + "/noffsetTop=" + xy.y);   
}   
</script>  
</head>  
<body>  
<input type="button" onclick="phplamp(this)" value="点击" />    
</body>  
</html>


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表