textbox 距页面上边框和左边框的距离

征服者X 2009-12-25 01:36:45
textbox 距页面上边框和左边框的距离
...全文
269 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
浪尖赏花 2009-12-25
  • 打赏
  • 举报
回复
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>test</title>
<script>
/*
* 获取元素距BODY上边框的距离
*/
function getTop(obj){
var height = 0;
while(obj.tagName.toUpperCase() != "BODY")
{
height+=obj.offsetTop;
obj = obj.offsetParent;
}
return height;
}

/*
* 获取元素距BODY左边框的距离
*/
function getLeft(obj){
var width = 0;
while(obj.tagName.toUpperCase() != "BODY")
{
width+=obj.offsetLeft;
obj = obj.offsetParent;
}
return width;
}

window.onload = function(){
var txt1 = document.getElementById("txt1");
alert(getTop(txt1));
alert(getLeft(txt1));
var txt2 = document.getElementById("txt2");
alert(getTop(txt2));
alert(getLeft(txt2));
};
</script>
</head>

<body>
<div style="position:absolute;left:100px;top:200px">
<input type=text value="" id="txt1"/>
<input type=text value="" id="txt2"/>
</div>
</body>

</html>
sohighthesky 2009-12-25
  • 打赏
  • 举报
回复

var getPosition=function(_obj) {
var x = 0, y = 0;
do{
x += _obj.offsetLeft;
y += _obj.offsetTop;
}while(_obj=_obj.offsetParent);
return {'x':x,'y':y};
};
hitlcyu19 2009-12-25
  • 打赏
  • 举报
回复
css吧 padding-top padding-left什么的

87,914

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧