首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > CSS >

JS取得HTML元素位置

2012-11-04 
JS获得HTML元素位置//获得元素位置function getAbsolutePosition(obj) {position new Object()position

JS获得HTML元素位置

//获得元素位置function getAbsolutePosition(obj) {        position = new Object();        position.x = 0;        position.y = 0;        position.h = 0;        position.w = 0;        var tempobj = obj;        if (tempobj.offsetHeight + '' != 'undefined') { position.h = tempobj.offsetHeight; }        if (tempobj.offsetWidth + '' != 'undefined') { position.w = tempobj.offsetWidth; }        while (tempobj != null && tempobj != document.body) {            position.x += tempobj.offsetLeft + tempobj.clientLeft;            position.y += tempobj.offsetTop + tempobj.clientTop;            tempobj = tempobj.offsetParent       }        return position;   }//使用var ask = getAbsolutePosition(xxx);alert(ask.x);   //横坐标alert(ask.y);   //纵坐标alert(ask.h);   //高alert(ask.w);   //宽

热点排行