昨天搞了一个日历选择器的小东西,竟然发现以前用的获取标签的座标的函数在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" value="点击" />
</body>
</html>