简体   繁体   中英

remove parent div leaving its child div

I have child div inside its parent.

How to remove parent div leaving only its child untouched?

I don't use jQuery.

<div id="parent">
    <div id="child">
    </div>
</div>

to become

<div id="child"></div>

Get the element, get its parentNode, replace it with the child.

 var child = document.getElementById('child'); child.parentNode.replaceWith(child); 
 <div id="parent"> <div id="child"> A </div> </div> 

If you have support issues with replaceWith you can try the insertBefore approach.

 var child = document.getElementById('child'); var parent = child.parentNode; parent.parentNode.insertBefore(child, parent); parent.parentNode.removeChild(parent); 
 <div id="parent"> <div id="child"> A </div> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM