function getAbsPoint(e)
{
var x = e.offsetLeft, y = e.offsetTop;
while(e=e.offsetParent){x += e.offsetLeft; y += e.offsetTop;}
return {"x": x, "y": y};
}<td onclick="var xy=getAbsPoint(this); alert('x='+ xy.x +' y='+ xy.y)">
{
var x = e.offsetLeft, y = e.offsetTop;
while(e=e.offsetParent){x += e.offsetLeft; y += e.offsetTop;}
return {"x": x, "y": y};
}<td onclick="var xy=getAbsPoint(this); alert('x='+ xy.x +' y='+ xy.y)">
解决方案 »
- 关于Extjs4的问题:panel中通过iframe嵌套网页,弹出的Window中有grid panel,结果显示两行一样的column标题
- js成功信息提示问题
- js空链接 javascript:void(null) 在IE6下会弹出窗口?
- JS DOM 如何获取触发事件的节点?
- 关于arcgis10.1中几何服务的问题
- 如何取得QQ空间的最近访客URL
- js文件打开乱码,该咋办
- html标签中有既可以选,又可以写的下拉列表框吗?
- 神啊!救救我吧,关于opener的问题
- 关于CHECKBOX控制文本框的问题
- ●●●100分●●●如何点一下按钮就在页面中创建一个DIV????
- 求助:如何用JAVASCRIPT指定一个控件,控件名带括号的?????????????
必须要加上上级元素的相对坐标吗?我试了,这样算出来的好像还是有点差距,差几个像素,有没有一个办法能直接获得向鼠标一样的绝对坐标呢?
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
div{text-align:center;}
</style>
</head><body style="margin:10;border:20px;" onclick="alert('body offsetTop:'+this.offsetTop)" bgcolor="eeeeee">
<div onclick="alert(this.offsetTop);alert(this.offsetParent.tagName)" style="background-color:red;border:2px;">click here to test for body</div>
一个对象相当于一个box,它包括以下几方面的空间:<br>
<div style="background-color:cccccc;width:200;"> margin
<div style="background-color:red;width:160;"> border
<div style="background-color:blue;width:120;padding:10;margin:10;" onclick="alert(this.offsetTop);alert('offsetHeight:'+this.offsetHeight)"> padding
<div style="background-color:white;" onclick="alert(this.offsetTop);"> 对象本身
</div>
</div>
</div>
</div>
<br>
以下,自己的初步总结,有待确认<br>
offetTop是自己的border top 相对于offsetParent border top的距离.<br>
(当offsetParent为body是,情况有点特殊,可能是body对象对border不大灵光所致).<br>
offsetHeight是整border的占的height,不包括margin的范围<br>
</body></html>
function getAbsPoint(obj)
{
var x,y;
oRect = obj.getBoundingClientRect();
x=oRect.left
y=oRect.top
alert("("+x+","+y+")")
}</script>
<table border=1><tr>
<td onclick="getAbsPoint(this)">rrrere</td>
</tr></table>
screen.width
screen.height2。获得窗口大小:
document.body.clientWidth
document.body.clientHeight3。获得窗口大小(包含Border、Scroll等元素)
document.body.offsetWidth
document.body.offsetHeight
div相关的一些元素详细说明可以参考这张图
====CSDN 小助手 V2.5 2005年11月05日发布====
CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
界面:http://blog.csdn.net/Qqwwee_Com/archive/2005/11/05/523395.aspx
下载:http://szlawbook.com/csdnv2
event.y+document.body.scrollTop