通过js怎么获得div的位置? <div align="center"></div>如何通过js获得上面这个层的位置 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /** * 坐标 * @param x * @param y * @return */ function CPos(x, y) { this.x = x; this.y = y; } /** * 得到对象的相对浏览器的坐标 * @param ATarget * @return */ function GetObjPos(ATarget) { var target = ATarget; var pos = new CPos(target.offsetLeft, target.offsetTop); var target = target.offsetParent; while (target) { pos.x += target.offsetLeft; pos.y += target.offsetTop; target = target.offsetParent } return pos; } <div id="test" style="position:absolute; left:100px; top:200px;">123</div><script>/** * 坐标 * @param x * @param y * @return */ function CPos(x, y) { this.x = x; this.y = y; } /** * 得到对象的相对浏览器的坐标 * @param ATarget * @return */ function GetObjPos(ATarget) { var target = ATarget; var pos = new CPos(target.offsetLeft, target.offsetTop); var target = target.offsetParent; while (target) { pos.x += target.offsetLeft; pos.y += target.offsetTop; target = target.offsetParent } return pos; } var obj = document.getElementById('test') alert(GetObjPos(obj)['y']) //y坐标 alert(GetObjPos(obj)['x']) //x坐标 </script>不错 <div align="center" id="div1"></div> <script>var div1=document.getElementById("div1");var divPos=GetObjPos(div1);alert(divPos.x);alert(divPos.y);</script> 简单点的 可以通过document.getElementById("DIV的ID").style.Top(Left)来获取元素据浏览器上端(左端)距离 即xy坐标位置 JS DOM中getElementByTagName("div")获取标签元素,object.getBoundingClientRect()方法获取坐标,通过left和top属性。参考下这个,希望对你有帮助http://www.jz123.cn/text/0626163.html 求助,如何使用js删除行,效果如下图所示 在线等JS高手解答兼容性的问题! js 递归获取dom某节点的所有子节点 js文件问题 ●●●●●●●●●●●●这个方法好像没有用啊????????? 如何判断checkbox是否至少有选择一项 [求助]请问如何得到window的高度和宽度???? 来给简单的给大家点分 用JAVASCRIPT去取得一个<input type="file" name="file">中的FILE的大小 vscode 语法检查太严格怎么解决? 正则表达式疑问 求高手优化js代码
/**
* 坐标
* @param x
* @param y
* @return
*/
function CPos(x, y)
{
this.x = x;
this.y = y;
}
/**
* 得到对象的相对浏览器的坐标
* @param ATarget
* @return
*/
function GetObjPos(ATarget)
{
var target = ATarget;
var pos = new CPos(target.offsetLeft, target.offsetTop);
var target = target.offsetParent;
while (target)
{
pos.x += target.offsetLeft;
pos.y += target.offsetTop;
target = target.offsetParent
}
return pos;
}
<div id="test" style="position:absolute; left:100px; top:200px;">123</div>
<script>/**
* 坐标
* @param x
* @param y
* @return
*/
function CPos(x, y)
{
this.x = x;
this.y = y;
}
/**
* 得到对象的相对浏览器的坐标
* @param ATarget
* @return
*/
function GetObjPos(ATarget)
{
var target = ATarget;
var pos = new CPos(target.offsetLeft, target.offsetTop);
var target = target.offsetParent;
while (target)
{
pos.x += target.offsetLeft;
pos.y += target.offsetTop;
target = target.offsetParent
}
return pos;
}
var obj = document.getElementById('test')
alert(GetObjPos(obj)['y']) //y坐标
alert(GetObjPos(obj)['x']) //x坐标
</script>
不错
</div>
<script>
var div1=document.getElementById("div1");
var divPos=GetObjPos(div1);
alert(divPos.x);
alert(divPos.y);
</script>
参考下这个,希望对你有帮助http://www.jz123.cn/text/0626163.html