动态创建DIV后位置出现问题!<div id="aa"><img src="http://bbs.51js.com/images/51js.gif" /></div>
<script>
function ss(){
var _div=document.createElement("div")
_div.style.overflow="hidden"
_div.appendChild(document.getElementById("aa"))
document.body.appendChild(_div)
}
</script><input type="button" value="button" onclick="ss()" />

解决方案 »

  1.   


    <div id="aa"><img src="http://bbs.51js.com/images/51js.gif" /></div>
    <script>
    位置动是很正常的``
    你以上的代码是把
    1。创建一个DIV 包含 aa
    2。把div aa 从HTML DOM中移除 [自动的]
    3。再把DIV加入到DOM元素中``有几种办法都可以解决
    1。使用绝对位置
    2。定义一个不动的元素为容器 如下:function ss(){
    var _div=document.createElement("div")
    document.all.x.style.overflow="hidden";
    _div.appendChild(document.getElementById("aa"))
    document.all.x.appendChild(_div)
    }
    </script>
    <div id='x' style='width:100px;height:20px;background:#def'></div>
    <input type="button" value="button" onclick="ss()" />