我是这样做的 var addpic=document.getElementById("addpic"); var tp=addpic.offsetTop; while (addpic.offsetParent != null) { oParent = addpic.offsetParent; tp += oParent.offsetTop; // Add parent top position addpic = oParent; } var uploadpic=document.getElementById("mmsuploaddiv"); uploadpic.style.top=tp; 在ie下可以将mmsuploaddiv覆盖到addpic上,但是在firefox下就不起作用了 firefox下要怎么做
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>
var addpic=document.getElementById("addpic");
var tp=addpic.offsetTop;
while (addpic.offsetParent != null) {
oParent = addpic.offsetParent;
tp += oParent.offsetTop; // Add parent top position
addpic = oParent;
}
var uploadpic=document.getElementById("mmsuploaddiv");
uploadpic.style.top=tp;
在ie下可以将mmsuploaddiv覆盖到addpic上,但是在firefox下就不起作用了
firefox下要怎么做
<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>