JS获取鼠标点击位置坐标

JavaScript013

JS获取鼠标点击位置坐标,第1张

是指Script生成的iframe吧?理论上,iframe内的页面和parent页只有上下级关系,所以鼠标在iframe内的坐标应该不能在父页获取。你可以用document.getElementById("iframe1").contentDocument得到iframe的document对象,之后操作它得到iframe内的坐标,再与父页中iframe标签左上角坐标叠加就可以。

js获取DIV的位置坐标的方法大概有两种:

第一种:编辑代码:var odiv=document.getElementById('divid')

alert(odiv.getBoundingClientRect().left)

alert(odiv.getBoundingClientRect().top)

第二种:编辑代码function CPos(x, y) {this.x = x this.y = y} /*** 得到对象的相对浏览器的坐标*/ function GetObjPos(ATarget {var target = ATargetvar pos = new CPos(target.offsetLeft, target.offsetTop)var target =target.offsetParentwhile (target  pos.x += target.offsetLeft  pos.y += target.offsetTop target = target.offsetParent }return pos }var obj =  document.getElementById('divid') alert(GetObjPos(obj)['x'])//x坐标alert(GetObjPos(obj)['y'])//y坐标

SPAN 和 DIV 的区别在于,DIV(division)是一个块级元素,可以包含段落、标题、表格,乃至诸如章节、摘要和备注等。而SPAN 是行内元素,SPAN 的前后是不会换行的,它没有结构的意义,纯粹是应用样式,当其他行内元素都不合适时,可以使用SPAN

点击的时候获取下当前位置

获取位置方法

clientX,clientY

screenX,screenY

具体例子看下

http://www.w3school.com.cn/jsref/event_clientx.asp

http://www.cnblogs.com/dolphinX/archive/2012/10/09/2717119.html