javascript取有些网页的html元素的offsetLeft,offsetTop数据不准,比如这个网页:http://www.google.com.hk/
function computeRange(o)
{
var p = { x: o.offsetLeft, y: o.offsetTop,x1:o.offsetWidth,y1:o.offsetHeight };
while (o = o.offsetParent)
{
p.x += o.offsetLeft;
p.y += o.offsetTop;
}
p.x1 += p.x;
p.y1 += p.y;
return p;
}
------解决方案--------------------------------------------------------
页面元素的属性,不同的浏览器或者不同的浏览器版本,解析是会有些不同的
------解决方案--------------------------------------------------------
貌似只有绝对定位的元素才有offset吧
------解决方案--------------------------------------------------------
<html>
<head>
<title>Chapter 3 Project</title>
<style type="text/css">
body
{
margin:0px;
padding:0px;
}
</style>
<script type="text/javascript">
window.onload = function() {
var obj = document.getElementById("loca");
findPos(obj);
}
function findPos(obj) {
var curleft = 0;
var curtop = 0;
if (obj.offsetParent) {
while (obj.offsetParent) {
curleft += obj.offsetLeft;
curtop += obj.offsetTop;
obj = obj.offsetParent;
}
}
return [curleft, curtop];
}
</script>
</head>
<body>
<div style="position:relative">
<div id="loca" style="position:absolute;left:100px;top:200px;">
</div>
</div>
</body>
</html>