昨天搞了一个日历选择器的小东西,竟然发现以前用的获取标签的坐标的函数在Firefox下不能执行,比较郁闷。于是花心思整理了一个,现此代码支持Ie,Firefox,Opera,记录于此!
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};
}
<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>
PHPLAMP博客是专注于网站建设,搜索引擎研究,网站推广,网站优化的IT博客。