admin管理员组文章数量:1399958
<div id = "a" > </div>
<div id ="b"> child </div>
so i have go two div's here i want to get div"b" inside div"a" if i type:
document.getElementById("a").innerHTML = document.getElementById("b").value;
it will give me this output
<div id = "a" > child </div>
but what i want is
<div id = "a" > <div id ="b"> child </div> </div>
<div id = "a" > </div>
<div id ="b"> child </div>
so i have go two div's here i want to get div"b" inside div"a" if i type:
document.getElementById("a").innerHTML = document.getElementById("b").value;
it will give me this output
<div id = "a" > child </div>
but what i want is
<div id = "a" > <div id ="b"> child </div> </div>
Share
Improve this question
asked Jun 27, 2017 at 20:03
Ng21Ng21
2312 gold badges4 silver badges11 bronze badges
2
- 1 BTW value is not a valid attribute for div. Maybe you meant innerHTML? – Andrew Li Commented Jun 27, 2017 at 20:05
- yes exactly buddy you are right – Ng21 Commented Jun 27, 2017 at 20:11
4 Answers
Reset to default 6Try appendChild()
;
document.getElementById('a').appendChild(document.getElementById('b'));
<div id = "a" ></div>
<div id ="b">child</div>
Use element's outerHTML as follows:
document.getElementById("a").innerHTML = document.getElementById("b").outerHTML;
In the time it took me to write this, other answers were made saying the same.
Run the snippet to see the code in action...
setTimeout(function() {
var a = document.getElementById("a");
var b = document.getElementById("b");
a.appendChild(b)
}, 1000)
.border {
border: 1px solid black;
width: 100%;
max-width: 50px;
text-align: center;
margin: 5px 0;
}
.border div{
width: 80%;
margin: 5px auto;
}
<div id="a" class="border">A</div>
<div id="b" class="border">B</div>
You can copy the tag b to tag a copying it outerHTML
document.getElementById("a").innerHTML = document.getElementById("b").outerHTML;
Then if you want to remove the b tag use
document.getElementById('b').remove();
本文标签: javascripthow to get div inside another div by calling documentgetElementbyIdStack Overflow
版权声明:本文标题:javascript - how to get div inside another div by calling document.getElementbyId - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744158756a2593235.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论