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

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>testtitle>
<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>

你可能感兴趣的:(javascript)