javascript 获取鼠标的绝对位置 event
New Document
Y:" + y;
}
function getX(elementID)
{
var el = elementID
return el.offsetLeft
}
function getY(elementID)
{
var el = elementID
return el.offsetTop
}
function getElementPositionX(elemID)
{
var offsetTrail = document.getElementById(elemID);
var offsetLeft = 0;
while(offsetTrail)
{
offsetLeft += offsetTrail.offsetLeft;
offsetTrail = offsetTrail.offsetParent;
}
if (navigator.userAgent.indexOf("Mac") != -1 &&
typeof(document.body.leftMargin) != "undefined") {
offsetLeft += document.body.leftMargin;
}
return offsetLeft;
}
function getElementPositionY(elemID)
{
var offsetTrail = document.getElementById(elemID);
var offsetTop = 0;
while(offsetTrail)
{
offsetTop += offsetTrail.offsetTop;
offsetTrail = offsetTrail.offsetParent;
}
if (navigator.userAgent.indexOf("Mac") != -1 &&
typeof(document.body.leftMargin) != "undefined") {
offsetTop += document.body.topMargin;
}
return offsetTop;
}
function getElementPositionXY(elemID)
{
var offsetTrail = elemID;
var offsetLeft = 24;
var offsetTop =0;
while(offsetTrail)
{
offsetLeft += offsetTrail.offsetLeft;
offsetTop += offsetTrail.offsetTop;
offsetTrail = offsetTrail.offsetParent;
}
if (navigator.userAgent.indexOf("Mac") != -1 &&
typeof(document.body.leftMargin) != "undefined") {
offsetLeft += document.body.leftMargin;
offsetLeft += document.body.leftMargin;
}
document.getElementById("txt_left").innerText = offsetLeft;
document.getElementById("txt_top").innerText = offsetTop;
document.getElementById("divMsg").style.top = offsetTop + "px";
}
//-->
鼠标的绝对位置:
控件输入框txtPos的绝对位置:
X:
Y:
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]