1. 首先在HTML的HEAD里加入:
<meta content="JavaScript" name="vs_defaultClientScript">
<script language="javascript" src="../../Script/persistlocation.js"></script>
2.写好上面目录下的javascript文件:
如:
/**********************************************************
* In order to get the position of HTML element,we provide *
* this file.It is a javascript code. *
* Written by Yuzhibin *
* Created on July 9 2004 *
* *
* *
***********************************************************/
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 = document.getElementById(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;
}
3. 在.CS下直接用下面的方式调用:
rhombusCalendar.pTop = "javascript:getX(btn_customerRequestDateFrom)";
或: btnCtrlAddress1.Attributes["onclick"] = "javascript:getElementPositionXY('"+lblCtrlAddress1.ClientID+"')";
本文介绍了一种使用JavaScript来获取HTML页面中元素位置的方法,包括水平和垂直坐标,并提供了具体实现的代码示例。
90

被折叠的 条评论
为什么被折叠?



