日期:2014-05-16  浏览次数:20375 次

当点击一个超链接的时候,如何获取其在页面中的坐标?
<div   >
<a   href= "url1 "> 11 </a>
<a   href= "url2 "> 22 </a>
<a   href= "url3 "> 33 </a>
<a   href= "url4 "> 44 </a>
<a   href= "url5 "> 55 </a>
<a   href= "url6 "> 66 </a>
</div>


当点击其中一个超链的时候,如何获取其坐标?
最好ie和ff下通用

谢谢

------解决方案--------------------
<div >
<a href= "#url1 "> 11 </a>
<a href= "#url2 "> 22 </a>
<a href= "#url3 "> 33 </a>
<a href= "#url4 "> 44 </a>
<a href= "#url5 "> 55 </a>
<a href= "#url6 "> 66 </a>
</div>

<script language= "javascript ">
function getPostion(e)
{
if(!e)
var e=window.event;
var obj = (e.target)? e.target : e.srcElement;
if(obj.tagName== "A ")
{
var top,left;
top=obj.offsetTop;
left=obj.offsetLeft;
while (obj = obj.offsetParent)
{
top+=obj.offsetTop;
left+=obj.offsetLeft;
if(obj.tagName== "BODY ")
{
alert( "x= "+left+ ";y= "+top);
return;
}
}
}
}
document.onclick=getPostion;
</script>
------解决方案--------------------
function Event(e){
var oEvent = (document.all) ? window.event : e;
if(document.all){
oEvent.pageX = oEvent.clientX + document.body.scrollLeft;
oEvent.pageY = oEvent.clientY + document.body.scrollTop;
}
return oEvent;
}