日期:2014-05-16  浏览次数:20458 次

如何用javascript改变元素的从属关系?
请先看下面的HTML:
<div   id= "div1 "   style= "border:1px   red   solid;width:150px;height:150px;background-color:#FFB9B9 "> </div>
<div   id= "div2 ">
Hello   world!
</div>
-------------------------------------
<div   id= "div3 "   style= "border:1px   red   solid;width:150px;height:150px;background-color:#FFB9B9 ">
<div   id= "div4 ">
Hello   world!
</div>
</div>

现在的问题是,如何用javascript把div2放到div1里面,从而实现像类似于div3和div4这样的从属关系?谢谢!

------解决方案--------------------
<div id= "div1 " style= "border:1px red solid;width:150px;height:150px;background-color:#FFB9B9 "> </div>
<div id= "div2 ">
Hello world!
</div>
<script language= "JavaScript ">
<!--
function tt()
{
var div1=document.getElementById( "div1 ");
var div2=document.getElementById( "div2 ");
div1.appendChild(div2.cloneNode(true));
document.body.removeChild(div2);
//alert(document.body.innerHTML);
}
tt();
//-->
</script>

------解决方案--------------------
document.getElementById( "div1 ").appendChild(document.getElementById( "div2 ")); //就一行代码