日期:2014-05-16 浏览次数:20543 次
uploadpic.style.top=tp+'px';//加单位试试
------解决方案--------------------
用getBoundingRect函数可以获得节点的绝对位置信息,但是设置节点的绝对位置好像没有什么好方法,如果节点的position为absolute时,style中的left和top可以用来绝对定位,下面是个例子
<html>
<head>
<script>
function test() {
var obj1 = document.getElementById("obj1");
var obj2 = document.getElementById("obj2");
var rect = obj1.getBoundingClientRect();
obj2.style.left = rect.left;
obj2.style.top = rect.top;
}
</script>
</head>
<body onload="test()">
<div id="obj1" style="position:absolute;left:200px;top:200px;width:100px;height:100px;background-color:red;"></div>
<div id="obj2" style="position:absolute;left:100px;top:100px;width:50px;height:50px;background-color:yellow;"></div>
</body>
</html>