资料
表格
制度
合同
管理
职场
经营
创业
范文
总结
计划
作文
文章
美文
词句
教程
模板
图片
字体
图标
办公
人事
财务
生产
管理资源吧首页
>>>
教程
>>>
编程
>>>
JavaScript教程
>>>
javascript 获取鼠标的绝对位置 event
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title> New Document </title> <meta name="Generator" content="EditPlus"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> </head> <body onmouseMove="getXY(event)"> <img src="/_New/_cj/myeditor/sysimage/space.gif" class="ewebeditor__script" _ewebeditor_fake_tag="script" _ewebeditor_fake_value="%3Cscript%20language%3D%22JavaScript%22%3E%20%0D%0A%3C!--%20%0D%0Afunction%20mouseX(evt)%20%7B%20%0D%0Aif%20(evt.pageX)%20return%20evt.pageX%3B%20%0D%0Aelse%20if%20(evt.clientX)%20%0D%0Areturn%20evt.clientX%20%2B%20(document.documentElement.scrollLeft%20%3F%20%0D%0Adocument.documentElement.scrollLeft%20%3A%20%0D%0Adocument.body.scrollLeft)%3B%20%0D%0Aelse%20return%20null%3B%20%0D%0A%7D%20%0D%0Afunction%20mouseY(evt)%20%7B%20%0D%0Aif%20(evt.pageY)%20return%20evt.pageY%3B%20%0D%0Aelse%20if%20(evt.clientY)%20%0D%0Areturn%20evt.clientY%20%2B%20(document.documentElement.scrollTop%20%3F%20%0D%0Adocument.documentElement.scrollTop%20%3A%20%0D%0Adocument.body.scrollTop)%3B%20%0D%0Aelse%20return%20null%3B%20%0D%0A%7D%20%0D%0Afunction%20getXY(event)%20%0D%0A%7B%20%0D%0Avar%20e%20%3D%20event%20%7C%7C%20window.event%3B%20%0D%0Avar%20x%20%3D%20mouseX(e)%3B%20%0D%0Avar%20y%20%3D%20mouseY(e)%3B%20%0D%0Adocument.getElementById(%22XY%22).innerHTML%20%3D%20%22X%3A%22%20%2B%20x%20%2B%20%22%3Cbr%3EY%3A%22%20%2B%20y%3B%20%0D%0A%7D%20%0D%0Afunction%20getX(elementID)%20%0D%0A%7B%20%0D%0Avar%20el%20%3D%20elementID%20%0D%0Areturn%20el.offsetLeft%20%0D%0A%7D%20%0D%0Afunction%20getY(elementID)%20%0D%0A%7B%20%0D%0Avar%20el%20%3D%20elementID%20%0D%0Areturn%20el.offsetTop%20%0D%0A%7D%20%0D%0Afunction%20getElementPositionX(elemID)%20%0D%0A%7B%20%0D%0Avar%20offsetTrail%20%3D%20document.getElementById(elemID)%3B%20%0D%0Avar%20offsetLeft%20%3D%200%3B%20%0D%0Awhile(offsetTrail)%20%0D%0A%7B%20%0D%0AoffsetLeft%20%2B%3D%20offsetTrail.offsetLeft%3B%20%0D%0AoffsetTrail%20%3D%20offsetTrail.offsetParent%3B%20%0D%0A%7D%20%0D%0Aif%20(navigator.userAgent.indexOf(%22Mac%22)%20!%3D%20-1%20%26%26%20%0D%0Atypeof(document.body.leftMargin)%20!%3D%20%22undefined%22)%20%7B%20%0D%0AoffsetLeft%20%2B%3D%20document.body.leftMargin%3B%20%0D%0A%7D%20%0D%0Areturn%20offsetLeft%3B%20%0D%0A%7D%20%0D%0Afunction%20getElementPositionY(elemID)%20%0D%0A%7B%20%0D%0Avar%20offsetTrail%20%3D%20document.getElementById(elemID)%3B%20%0D%0Avar%20offsetTop%20%3D%200%3B%20%0D%0Awhile(offsetTrail)%20%0D%0A%7B%20%0D%0AoffsetTop%20%2B%3D%20offsetTrail.offsetTop%3B%20%0D%0AoffsetTrail%20%3D%20offsetTrail.offsetParent%3B%20%0D%0A%7D%20%0D%0Aif%20(navigator.userAgent.indexOf(%22Mac%22)%20!%3D%20-1%20%26%26%20%0D%0Atypeof(document.body.leftMargin)%20!%3D%20%22undefined%22)%20%7B%20%0D%0AoffsetTop%20%2B%3D%20document.body.topMargin%3B%20%0D%0A%7D%20%0D%0Areturn%20offsetTop%3B%20%0D%0A%7D%20%0D%0Afunction%20getElementPositionXY(elemID)%20%0D%0A%7B%20%0D%0Avar%20offsetTrail%20%3D%20elemID%3B%20%0D%0Avar%20offsetLeft%20%3D%2024%3B%20%0D%0Avar%20offsetTop%20%3D0%3B%20%0D%0Awhile(offsetTrail)%20%0D%0A%7B%20%0D%0AoffsetLeft%20%2B%3D%20offsetTrail.offsetLeft%3B%20%0D%0AoffsetTop%20%2B%3D%20offsetTrail.offsetTop%3B%20%0D%0AoffsetTrail%20%3D%20offsetTrail.offsetParent%3B%20%0D%0A%7D%20%0D%0Aif%20(navigator.userAgent.indexOf(%22Mac%22)%20!%3D%20-1%20%26%26%20%0D%0Atypeof(document.body.leftMargin)%20!%3D%20%22undefined%22)%20%7B%20%0D%0AoffsetLeft%20%2B%3D%20document.body.leftMargin%3B%20%0D%0AoffsetLeft%20%2B%3D%20document.body.leftMargin%3B%20%0D%0A%7D%20%0D%0Adocument.getElementById(%22txt_left%22).innerText%20%3D%20offsetLeft%3B%20%0D%0Adocument.getElementById(%22txt_top%22).innerText%20%3D%20offsetTop%3B%20%0D%0Adocument.getElementById(%22divMsg%22).style.top%20%3D%20offsetTop%20%2B%20%22px%22%3B%20%0D%0A%7D%20%0D%0A%2F%2F--%3E%20%0D%0A%3C%2Fscript%3E"> 鼠标的绝对位置:<br> <span id="XY" style="color:red;"></span><br><br> 控件输入框txtPos的绝对位置:<br> X:<span id="txt_left" style="color:red;"></span><br> Y:<span id="txt_top" style="color:red;"></span><br> <input type="text" name="txtPos" id="txtPos" onfocus="getElementPositionXY(this)"/> <br><br> <div id="divMsg" style="height:60px;width:100px;background-color:#cccccc"></div> </body> </html>
[Ctrl+A 全选 注:
如需引入外部Js需刷新才能执行
]
教程首页
更多教程