<DIV onclick="aa(this)"></DIV><script language="javascript">
function aa(obj)
{
var pos = getAbsolutePosition(obj);
alert((pos.x - parsePx(obj.style.left)));
alert((pos.y - parsePx(obj.style.top)));
}function parsePx(inputstr)
{
var tempstr = inputstr.replace("px","");
var tempint = parseInt(tempstr);
return isNaN(tempint)?0:tempint;
}function getAbsolutePosition(obj)
{
position = new Object();
position.x = 0;
position.y = 0;
var tempobj = obj;
while(tempobj!=null && tempobj!=document.body)
{
position.x += tempobj.offsetLeft + tempobj.clientLeft;
position.y += tempobj.offsetTop + tempobj.clientTop;
tempobj = tempobj.offsetParent
}
position.x += document.body.scrollLeft;
position.y += document.body.scrollTop;
return position;
}
</script>
function aa(obj)
{
var pos = getAbsolutePosition(obj);
alert((pos.x - parsePx(obj.style.left)));
alert((pos.y - parsePx(obj.style.top)));
}function parsePx(inputstr)
{
var tempstr = inputstr.replace("px","");
var tempint = parseInt(tempstr);
return isNaN(tempint)?0:tempint;
}function getAbsolutePosition(obj)
{
position = new Object();
position.x = 0;
position.y = 0;
var tempobj = obj;
while(tempobj!=null && tempobj!=document.body)
{
position.x += tempobj.offsetLeft + tempobj.clientLeft;
position.y += tempobj.offsetTop + tempobj.clientTop;
tempobj = tempobj.offsetParent
}
position.x += document.body.scrollLeft;
position.y += document.body.scrollTop;
return position;
}
</script>
function aa(obj)
{
alert((window.event.x - parsePx(obj.style.left)));
alert((window.event.y - parsePx(obj.style.top)));
}function parsePx(inputstr)
{
var tempstr = inputstr.replace("px","");
var tempint = parseInt(tempstr);
return isNaN(tempint)?0:tempint;
}
你写的时候可以这样写就兼容了ie和firefox
var _offsetX=offsetX || layerX
var _offsetY=offsetY || layerY