现在的位置: 首页 > 综合 > 正文

js如何往div里写div

2013年07月19日 ⁄ 综合 ⁄ 共 604字 ⁄ 字号 评论关闭

原有html代码:
...
<div id="div_1"> </div>
...

我现在想在这个div里写 <div id="div_2"> </div>.

完成后的效果如下:
<div id="div_1"> <div id="div_2"> </div> </div>
这个用js怎么实现?
谢谢!

 

两个div都存在 document.getElementById('div_1').appendChild(document.getElementById('div_2)); div_1存在,div_2不存在 var div_2 = document.createElement('div'); div_2.id = 'div_2'; document.getElementById('div_1').appendChild(div_2);

 

 

一。 document.getElementById('div_1').appendChild(document.getElementById('div_2));

div_1存在,div_2不存在 var div_2 = document.createElement('div'); div_2.id = 'div_2'; document.getElementById('div_1').appendChild(div_2); 二。 document.getElementById("div").innerHTML='

aaaa

'; 2选1

【上篇】
【下篇】